2012年7月26日 星期四

極大值問題


Problem2(11%)
有一個n位數的正整數A,請問刪除其中k個連續或不連續的位數 (<n)、將剩下的數字依序合併形成一個新的正整數BB的最大可能值是多少?(請注意,AB的首位都不能是0)。
輸入說明:
奇數列有兩個以空白隔開的正整數nk (1 ≤ ≤ 1000000)。偶數列會有一個長度為n的正整數A
輸出說明:
請輸出B的最大可能值。
輸入範例:
6 4
268574
5 2
41235
輸出範例:
87
435

3 則留言:

  1. 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, n1
    Input #1, x
    b = n - n1
    ans = ""
    a = 0
    Do
    b = b - 1
    Max = 0
    For i = a + 1 To n - b
    If Mid(x, i, 1) > Max Then Max = Mid(x, i, 1): a = i
    Next
    ans = ans & Max
    Loop Until b = 0
    Print #2, ans
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  2. 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, A
    B = ""
    x = n - k
    y = 0
    Do Until x = 0
    Max = 0
    x = x - 1
    For i = y + 1 To n - x
    If Mid(A, i, 1) > Max Then Max = Mid(A, i, 1): y = i
    Next i
    B = B & Max
    Loop
    Print #2, B
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. 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, x, y
    Input #1, z
    l = Val(x) - Val(y)
    Do Until Len(z) = l
    min1 = 99
    For i = 1 To Len(z)
    If Val(Mid(z, i, 1)) < min1 Then min1 = Mid(z, i, 1)
    Next i
    z = Replace(z, min1, "")
    Loop
    Print #2, z
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除