張三把所有的1000個盤子分別裝在10個箱子裡,每個箱子分別裝進1、2、4、8、
16、32、64、128、256、及489個盤子。張三將這些箱子依序標上(1)~(10)10個號
碼。有天,李四想找張三借n個盤子(n為程式使用者輸入的資料,代表李四欲借盤
子的數目,其限制範圍為1<=n<=1000,n為正整數)。張三知道每個箱子的編號與
箱子內所放盤子的個數,但如何設計一個程式,在不拆開箱子重新組合盤子的情
況下,告訴張三應組合拿出那些箱子給李四,才能得到李四要借的盤子數目呢?
範例:
輸入:
717
輸出:
(10)489
(8)128
(7)64
(6)32
(3)4
網誌管理員已經移除這則留言。
回覆刪除網誌管理員已經移除這則留言。
回覆刪除Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
Dim a(10) As Integer
For I = 1 To 9
a(I) = 2 ^ (I - 1)
Next I
a(10) = 489
Max = 10
Do Until x <= 0
If x >= a(Max) Then
x = x - a(Max)
Print #2, "("; Max; ")"; a(Max)
End If
Max = Max - 1
Loop
Close #1
Close #2
End Sub
網誌管理員已經移除這則留言。
回覆刪除Dim a(10) As Long
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
For i = 0 To 8
a(i + 1) = 2 ^ i
Next i
a(10) = 489
For i = 10 To 1 Step -1
If x - a(i) >= 0 Then
x = x - a(i)
Print "(" & i & ")" & a(i)
End If
Next i
Close #1
Close #2
End Sub
Private Sub Form_Load()
回覆刪除Dim A(11) As Integer
Open App.Path & "/in.txt" For Input As #1
Open App.Path & "/out.txt" For Output As #2
Input #1, N
If N >= 489 Then N = N - 489: Print #2, "(10)489"
For i = 8 To 0 Step -1
If N >= 2 ^ i Then N = N - 2 ^ i: Print #2, "(" & i + 1 & ")" & 2 ^ i
Next i
Close #2
Close #1
End Sub
Y揚、阿瑋、高仔好,
回覆刪除這題也因為討論過,程式是都沒問題。
Dim a(10) As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
a(1) = 1
For i = 2 To 9
a(i) = a(i - 1) * 2
Next i
a(10) = 489
c = 10
Input #1, x
Do Until x <= 0
If x >= a(c) Then
x = x - a(c)
Print #2, "(" & c & ")" & a(c)
End If
c = c - 1
Loop
Close #2
Close #1
End Sub