2011年10月21日 星期五

97正式 Problem4:排列最小值

Problem4:排列最小值(13%)

一串數字內有n個正整數。試著利用連接字串的方式去排列這n個正整數,使其值最小。

輸入說明:

每組輸入包含一串數字,內有n個正整數,數字與數字間以一個空白隔開。

輸出說明:

每組輸出排列後的最小正整數。

輸入範例:

1 36 3647

8 79 797 799

輸出範例:

1363647

797797998

6 則留言:

  1. Dim X() As String
    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)
    Line Input #1, Q
    X = Split(Q)

    For i = 0 To UBound(X) - 1
    For j = i + 1 To UBound(X)
    If ABC(X(i), X(j)) = False Then
    T = X(i)
    X(i) = X(j)
    X(j) = T
    End If
    Next j
    Next i

    ans = ""
    For i = 0 To UBound(X)
    ans = ans & X(i)
    Next i
    Print #2, ans

    Loop
    Close #2
    Close #1
    End
    End Sub

    Function ABC(A, B) As Boolean
    P = True

    If Len(A) = Len(B) Then
    If A > B Then P = False
    Else
    L = Abs(Len(B) - Len(A))
    If A > B Then P = False
    If P = True Then If Len(A) > Len(B) Then If Right(A, L) > Left(A, L) Then P = False
    If P = True Then If Len(A) < Len(B) Then If Right(B, L) < Left(B, L) Then P = False
    End If

    ABC = P
    End Function

    回覆刪除
  2. Dim s
    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
    Line Input #1, m
    s = Split(m)
    Call A1("", UBound(s) + 1, 0)
    Call A2
    Loop
    Close
    Close
    End
    End Sub

    Sub A1(a, b, c)
    If c = b Then
    Dim kk, q
    kk = ""
    q = Split(a)
    For i = 0 To UBound(q): kk = kk & q(i): Next
    List1.AddItem kk
    Else
    For i = 0 To UBound(s)
    If Instr2(a, s(i)) = 0 Then Call A1(a & " " & s(i), b, c + 1)
    Next
    End If
    End Sub


    Sub A2()
    Print #2, List1.List(0)
    End Sub

    Function Instr2(a, b)
    Dim AA
    AA = Split(a)
    Instr2 = 0
    For i = 0 To UBound(AA)
    If b = AA(i) Then Instr2 = 1
    Next
    End Function

    回覆刪除
  3. Private Sub Form_Activate()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    s = ""
    Line Input #1, n
    m = Split(n, " ")
    For i = 0 To UBound(m) - 1
    For j = i + 1 To UBound(m)
    If m(i) & m(j) > m(j) & m(i) Then Call swap(m(i), m(j))
    Next
    Next
    For i = 0 To UBound(m)
    s = s & m(i)
    Next
    Print #2, s
    Loop
    End Sub

    Function swap(A, B)
    c = A
    A = B
    B = c
    End Function

    回覆刪除
  4. 我想請問大大
    題目輸出的797797998
    是不是錯了?
    我用路克的方式去寫
    答案是797977998

    回覆刪除
  5. 哈囉您好

    題目要求的是 排列後 數字最小的組合。

    參考答案797797998 這個比較大
    您的答案797977998

    所以答案是沒有錯的唷


    還原排列組合

    參考答案 79 797 799 8
    您的答案 797 79 799 8

    很久沒有回來這裡了

    上大學有個小作業 回來看看以前如何寫的 :)

    回覆刪除
  6. 哦原來是這樣啊我知道了
    謝謝柯佑~

    回覆刪除