原始碼:http://down.gogobox.com.tw/t2329175/sq2m2
給定N個正整數。試著利用連接字串的方式去排列這N個正整數,使其值最大。

輸入範例:

5 123 124 56 90 9
5 28 285 287 2851 2859
2 25 2523
3 89 898 899

輸出範例:

99056124123
2872859285285128
252523
89989898
程式碼:
Option Explicit
Private Sub Command1_Click()
Dim i As Double, j As Double, k As Double, z As Double, h As Double
Dim c() As String, c() As String
Dim Ma As String, Mb As String, Temp As Double
c = Split(Right(Text1.Text, Len(Text1) - 2), " ")
z = UBounc(c)
For i = 0 To z
    Ma = Ma & c(i)
Next i
For i = 0 To z - 1
    For j = 0 To z - 1
        Temp = c(j): c(j) = c(j + 1): c(j + 1) = Temp
        Mb = ""
        For k = 0 To z
            Mb = Mb & c(k)
        Next
        If Val(Ma) < Val(Mb) Then
            Ma = Mb
        Else
            Temp = c(j): c(j) = c(j + 1): c(j + 1) = Temp
        End If
Next j, i
For i = 0 To z - 1
    If Len(c(i)) = Len(c(i + 1)) And Val(c(i)) < Val(c(i + 1)) Then Temp = c(i): c(i) = c(i + 1): c(i + 1) = Temp
Next
T2 = ""
For j = 0 To z
    T2 = T2 & c(j)
Next
Print
End Sub


創作者介紹
創作者 讓地獄深紅的天亮 的頭像
讓地獄深紅的天亮

!壞人必需做好事!

讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()