內容 :
給定N個正整數。試著利用連接字串的方式去排列這N個正整數,使其值最大。
輸入說明 :
每組輸入包含一串數字,第一個數字為 n,後面接著 n 個正整數
輸出說明 :
每組輸出排列後的最大正整數
範例輸入 :help
5 123 124 56 90 9
5 28 285 287 2851 2859
2 25 2523
3 89 898 899
範例輸出 :
99056124123
2872859285285128
252523
89989898
出處 :
96高市資訊學科能力競賽
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do While Not EOF(1)
Input #1, N
ReDim x(N)
For i = 1 To N
Input #1, x(i)
Next i
For i = 1 To N - 1
For j = i + 1 To N
Call ABC(x(i), x(j))
Next j
Next i
ans = ""
For i = 1 To N
ans = ans & x(i)
Next i
Print #2, ans
Loop
Close #2
Close #1
End
End Sub
Sub ABC(A, B)
win = A
If Len(A) > Len(B) Then
C = Len(A) - Len(B)
If Right(A, C) > Left(A, C) Then win = A
End If
If Len(A) < Len(B) Then
C = Len(B) - Len(A)
If Right(B, C) > Left(B, C) Then win = B
End If
If Len(A) >= Len(B) Then L = Len(B) Else L = Len(A)
If Right(A, L) > Right(B, L) Then win = A
If Right(A, L) < Right(B, L) Then win = B
If win = B Then
T = A
A = B
B = T
End If
End Sub
Dim X, n, t
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do While Not EOF(1)
List1.Clear
List2.Clear
Line Input #1, n
X = Split(n)
Call A1("", 0)
Call A2
Print #2, List2.List(List1.ListCount - 1)
Loop
Close
Close
End
End Sub
Sub A1(m, t)
If t = Val(X(0)) Then
List1.AddItem m
Else
For i = X(0) To 1 Step -1
If FackInstr(m, X(i)) = 0 Then Call A1(X(i) & " " & m, t + 1)
Next
End If
End Sub
Sub A2()
Dim AA, NA
For i = 0 To List1.ListCount - 1
a = List1.List(i)
NA = ""
AA = Split(a)
For j = 0 To UBound(AA)
NA = NA & AA(j)
Next
List2.AddItem NA
Next
End Sub
Function FackInstr(a, b)
FackInstr = 0
Dim OP
OP = Split(a)
For i = 0 To UBound(OP)
If b = OP(i) Then FackInstr = 1
Next
End Function
arro,佑好,
回覆刪除雖然你們兩個的程式都正確,但是,
都走了很常的繞路了。
沒有錯,就是要排序,但是,比大小要用「字串」的比法。
你們可以將陣列設成字串的陣列,再去排序。
更容易些的,可以將數字放進listbox,直接將sort屬性設true,不就排好了。