Largest int from concatenated ints: Difference between revisions

replace incorrect FreeBASIC solution with a correct one
(Add min)
(replace incorrect FreeBASIC solution with a correct one)
Line 688:
 
=={{header|FreeBASIC}}==
<lang freebasic>'#define versionMAXDIGITS 17-01-20168
' compile with: fbc -s console
 
function catint( a as string, b as string ) as uinteger
' TRUE/FALSE are built-in constants since FreeBASIC 1.04
return valint(a+b)
' But we have to define them for older versions.
end function
#Ifndef TRUE ' if TRUE is not defined then
#Define FALSE 0
#Define TRUE Not FALSE
#EndIf
 
function grt( a as string, b as string ) as boolean
Dim As Integer array()
return catint(a, b)>catint(b, a)
end function
 
sub shellsort( a() as string )
Function largest(array() As Integer) As String
'quick and dirty shellsort, not the focus of this exercise
dim as uinteger gap = ubound(a), i, j, n=ubound(a)
dim as string temp
do
gap = int(gap / 2.2)
for i=gap to n
temp = a(i)
j=i
while j>=gap andalso grt( a(j-gap), temp )
a(j) = a(j - gap)
j -= gap
wend
a(j) = temp
next i
loop until gap = 1
end sub
 
sub sort_and_print( a() as string )
Dim As Integer lb = LBound(array), ub = UBound(array)
Dimdim Asas Integeruinteger i, flag
Dimdim Asas Stringstring a_str(lboutstring To= ub),tmp""
shellsort(a())
for i=0 to ubound(a)
outstring = a(i)+outstring
next i
print outstring
end sub
 
dim as string set1(8) = {"1", "34", "3", "98", "9", "76", "45", "4"}
For i = lb To ub
dim as string set2(4) = {"54", "546", "548", "60"}
a_str(i) = Left(Str(array(i)) & String(14, " "), 14)
Next
 
sort_and_print(set1())
Do
sort_and_print(set2())</lang>
flag = TRUE
For i = lb To ub - 1
If a_str(i) < a_str(i+1) Then
Swap a_str(i), a_str(i +1)
flag = FALSE
End If
Next
If flag = TRUE Then Exit Do
Loop
 
For i = lb To ub
tmp += Trim(a_str(i))
Next
 
Return tmp
 
End Function
 
' ------=< MAIN >=------
 
Data 1, 34, 3, 98, 9, 76, 45, 4, -999
Data 54, 546, 548, 60, -999
Data -999
 
Dim As Integer i, x
 
Do
ReDim array(1 To 1)
i = 1
Do
Read x
If x = -999 Then Exit Do
If i > 1 Then
ReDim Preserve array(1 To i)
End If
array(i) = x
i += 1
Loop
If i = 1 Then Exit Do
Print "Largest concatenated int from {";
For i = lBound(array) To UBound(array)
Print Str(array(i));
If i = UBound(array) Then
Print "} = "; largest(array())
Else
Print ",";
End If
Next
Loop
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>998764543431
<pre>Largest concatenated int from {1,34,3,98,9,76,45,4} = 989764543431
6054854654
Largest concatenated int from {54,546,548,60} = 6054854654</pre>
</pre>
 
=={{header|Gambas}}==
781

edits