2011年6月20日 星期一

題4 剩下的最大數

有一個N位數的正整數A,請問刪除其中K個連續或不連續的位數(K
輸入說明:
奇樹列有兩個以空白格開的正整數N以及K( 1<=K<N<=1000000 )。偶數列會有一個長度為N的正整數A

輸出說明:
請輸出B的最大可能值

輸入範例:
6 4
268574
5 2
41235

輸出範例:
87
435

7 則留言:

  1. 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
    nn = ""
    Input #1, a, b
    Input #1, n

    c = a - b

    For i = 1 To Len(n)
    nn = nn & " " & Mid(n, i, 1)
    Next

    s = Split(nn)

    Call Q("", c, 0)

    ou = List1.List(List1.ListCount - 1)
    Do
    ou = Right(ou, Len(ou) - 1)
    Loop Until Left(ou, 1) <> 0

    Print #2, ou
    Loop
    Close
    Close
    End
    End Sub

    Function Q(ans As String, a, m)

    If Len(ans) = a Then

    Do Until Len(ans) = 10
    ans = "0" & ans
    Loop
    List1.AddItem ans

    Else
    For i = 0 To UBound(s)
    If m < i Then Call Q(ans & s(i), a, i)
    Next
    End If


    End Function

    回覆刪除
  2. arro好,
    程式有小錯誤,那個設定長度最長為10,為何不用a呢。
    還是先算20分,但是,再說一次,這樣的小錯誤,到時候是會哭的。

    回覆刪除
  3. Dim X As String
    Dim N As Byte
    Dim B As Byte
    Dim ans 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)
    Input #1, N, K
    Input #1, SN
    X = CStr(SN)
    N = N - K '需要幾個
    A = 9
    ans = ""

    Do

    B = Find_b(A)

    Select Case B
    Case Is = N: ans = ans & Right(X, B)
    Case Is < N: A = A - 1
    Case Is > N: X = Right(X, B - 1): ans = ans & CStr(A): N = N - 1: A = 9
    End Select

    Loop Until N = 0 Or N = B

    Print #2, ans

    Loop
    Close #2
    Close #1

    End
    End Sub

    Function Find_b(A)
    If InStr(X, A) <> 0 Then Find_b = (Len(X) - InStr(X, A) + 1)
    End Function

    老師寫的演算法,再複習一次。

    回覆刪除
  4. 用自己的方法寫,
    list2 屬性 Sorted = True

    老師明天早上我要去頒獎無法過去練習。

    Dim N As Long
    Dim K As Long

    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
    Input #1, N, K
    Input #1, A

    For i = 1 To Len(A)
    List1.AddItem Mid(A, i, 1)
    Next i


    Call ABC("", 0)

    Print #2, List2.List(List2.ListCount - 1)
    Loop
    Close #2
    Close #1

    End
    End Sub


    Sub ABC(X, ByVal Y)
    If Len(X) = N - K Then
    List2.AddItem X
    Else
    For i = 0 To (List1.ListCount - 1)
    If InStr(X, List1.List(i)) = 0 And i >= Y Then Call ABC(X & List1.List(i), i)
    Next i
    End If
    End Sub

    回覆刪除
  5. 佑好,
    第1個程式ok,
    但是第2個程式中,倒數第5行,的InStr(X, List1.List(i)) = 0,如果給的數字是有很多重複的呢?像是
    6 3
    929987

    回覆刪除
  6. 熊掌好,已解決
    謝老師。

    Dim N As Long
    Dim K As Long

    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
    Input #1, N, K
    Input #1, A

    For i = 1 To Len(A)
    List1.AddItem Mid(A, i, 1)
    Next i


    Call ABC("", -1)

    Print #2, List2.List(List2.ListCount - 1)
    Loop
    Close #2
    Close #1

    End
    End Sub


    Sub ABC(X, ByVal Y)
    If Len(X) = N - K Then
    List2.AddItem X
    Else
    For i = 0 To (List1.ListCount - 1)
    If i > Y Then Call ABC(X & List1.List(i), i)
    Next i
    End If
    End Sub
    --------------------------------
    in.txt
    6 4
    268574
    7 3
    9977789
    6 3
    929987
    --------------------------------
    out.txt
    87
    9989
    999

    回覆刪除
  7. 佑好,
    先祝一聲暑假快樂。
    這一題正確。
    暑假至少要一星期5題哦,當然是多多益善。
    如果找不到題目,可以留言說一下哦。
    另外,我應該7/21、22會到學校,也可以那時候去學校問問題哦。
    (飛往遠方的熊掌)

    回覆刪除