張三把所持有的1000個盤子分別裝在10個箱子裡,每個箱子分別裝進1、2、4、8、16、32、64、128、256、及489個盤子。張三將這些箱子依序標上1~10的號碼。有天,李四想找張三借N個盤子,張三知道每個箱子的編號與箱子內所放盤子的個數,但如何設計一個程式,在不拆開箱子重新組合盤子的情況下,告訴張三應拿出那些箱子給李四,才能得到李四要借的盤子數目呢?
輸入說明:每列中的數字代表要借的盤子數,當為0時表示結束。(請參照輸入範例)
輸入範圍:每列中的數字皆大於0,小於等於1000。
輸入範例:in.txt
717
329
0
第 3 頁/共 5 頁
輸出說明:每個數字代表箱子的編號,每個數字與數字間的區隔為一個空白符號,請由大到小排列。(請參照輸出範例)
輸出範例:out.txt
10 8 7 6 3
9 7 4 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
a = Split("0 1 2 4 8 16 32 64 128 256 489")
Do
Input #1, n
ans = n
For i = 10 To 1 Step -1
ans = ans - a(i)
If ans < 0 Then
ans = ans + a(i)
Else
Print #2, i;
End If
Next
Print #2,
Loop Until n = 0
Close
Close
End
End Sub
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
a = Split("1 2 4 8 16 32 64 128 256 489")
Do
Input #1, n
If n = 0 Then Exit Do
For i = 9 To 0 Step -1
n = n - a(i)
If n < 0 Then
n = n + a(i)
Else
Print #2, (i + 1);
End If
Next
Print #2,
Loop
Close
Close
End
End Sub
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
a = Split("0 1 2 4 8 16 32 64 128 256 489")
Do
Input #1, n
If n = 0 Then Exit Do
c = n
For i = 10 To 1 Step -1
c = c - a(i)
If c < 0 Then
c = c + a(i)
Else
Print #2, i;
End If
Next i
Print #2,
Loop Until n = 0
Close #2
Close #1
End
End Sub
程式正確
回覆刪除Dim a(10) As Integer
回覆刪除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
If x = 0 Then Exit Do
For i = 0 To 8
a(i + 1) = 2 ^ i
Next i
a(10) = 489
d = Val(x): strA = ""
For i = 10 To 1 Step -1
If d >= a(i) Then d = d - a(i): strA = strA & i & " "
If d = 0 Then Exit For
Next i
Print #2, strA
Loop
Close #2
Close #1
End
End Sub