Largest int from concatenated ints: Difference between revisions

no edit summary
(added FreeBASIC)
No edit summary
Line 595:
<pre>Largest concatenated int from {1,34,3,98,9,76,45,4} = 989764543431
Largest concatenated int from {54,546,548,60} = 6054854654</pre>
 
=={{header|Gambas}}==
<lang gambas>'Largest int from concatenated ints
 
Public Sub Form_Open()
Dim iList1 As Integer[] = [1, 34, 3, 98, 9, 76, 45, 4] 'Integer list 1
Dim iList2 As Integer[] = [54, 546, 548, 60] 'Integer list 2
 
Calc(iList1) 'Send List 1 to Calc routine
Calc(iList2) 'Send List 2 to Calc routine
 
End
_________________________________________________________________________________________
 
Public Sub Calc(iList As Integer[])
Dim siCount1, siCount2, siCounter As Short 'Counters
Dim sList As New String[] 'To hold converted integers
Dim bTrigger As Boolean 'To trigger a found match
 
For Each siCount1 In iList 'For each integer in the list..
sList.Add(Str(siCount1)) 'Convert to a string and add to sList
If Len(Str(siCount1)) > siCounter Then 'If the length of the string is greater than siCounter then..
siCounter = Len(Str(siCount1)) 'siCounter = length of the string
End If
Next
 
For siCount1 = 0 To sList.Max 'For each item in sList
If Len(sList[siCount1]) < siCounter Then 'If the length of the string is less that siCounter then..
sList[siCount1] &= Right(sList[siCount1], 1) 'Add the same digit to the string e.g. in list 1 "9" becomes "99", list 2 "54" becomes "544"
End If
Next
 
sList.Sort(gb.Descent) 'Sort the list in decending order
 
For siCount1 = 0 To sList.Max 'For each item in sList
bTrigger = False 'Set bTrigger to False
For siCount2 = 0 To iList.Max 'Loop through each item in iList
If Val(sList[siCount1]) = iList[siCount2] Then 'If the value of each is the same e.g. "98" = 98 then
bTrigger = True 'Set bTrigger to True
Continue 'Exit the loop
Endif
Next
If Not bTrigger Then 'If there was no match e.g. there is no "99" then..
sList[siCount1] = Left(sList[siCount1], siCounter - 1) 'Strip out the end digit e.g. "99" becomes 9 again
End If
Next
 
Print Val(sList.Join("")) 'Join all items in sList together and print
 
End</lang>
Output:
<pre>
998764543431
6054854654
</pre>
 
=={{header|Go}}==
Anonymous user