2010年2月9日 星期二

2010/02/08 攜帶容器裝水

現有容器容量分別為3公升、5公升、10公升、20公升以及40公升等五種,容器個數不限。輸入一欲帶回的公升數,計算出需攜帶的容器容量最小(x)、個數最少(y)與未裝滿容量最小(z),x+y+z最小之組合,若有相同者,均需要列出。
輸入格式:0~60之整數
輸出格式:(總個數):由大至小排列使用容器。

輸入範例:27
輸出範例:(3):20  5  3

出自  程式設計隊訓練教材 No.70 攜帶容器裝水

14 則留言:

  1. 輸入4 輸出 (2) 3 3
    輸入41 輸出(2) 40 3
    Private Sub Form_Load()
    Dim a(5) As Integer
    a(5) = 3: a(4) = 5: a(3) = 10: a(2) = 20: a(1) = 40
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    Close #1
    n = 1
    m = 0
    Do Until x <= 3
    If x >= a(n) Then
    m = m + 1
    x = x - a(n)
    ans = ans & a(n) & Space(1)
    End If
    n = n + 1
    Loop
    If x > 0 Then ans = ans & a(5): m = m + 1
    Print #2, "(" & m & ")" & " " & ans
    Close #2
    End Sub

    回覆刪除
  2. Dim ans As String
    Dim n, a(5) As Integer
    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
    a(1) = 40: a(2) = 20: a(3) = 10: a(4) = 5: a(5) = 3
    Do
    For i = 1 To 5
    If x >= a(i) Then
    x = x - a(i)
    ans = ans & " " & a(i)
    n = n + 1
    Exit For
    End If
    Next i
    Loop Until x < 3
    If x > 0 Then ans = ans & " " & a(5): n = n + 1
    Print #2, "(" & n & ")" & ":" & ans
    Close #1
    Close #2
    End Sub

    回覆刪除
  3. Y揚好,
    1、你的輸入輸出,有錯吧。如果輸入4 輸出 (1) 5,我算算x5 y1 z1與輸出(2) 3 3,x3 y2 z2哦,兩個x+y+z都是7哦。所以,這兩組都是答案囉。
    2、你的程式中,並未考慮一組以上的答案。

    阿瑋好,
    1、你的程式基本上和Y揚的相同。所以,問題也相同。

    另外,這題的x,指的是什麼?
    是攜帶的容器中,容量最小的那一個(x),
    還是
    是攜帶的容器,(總)容量最小(x)
    ,題意不是很清楚。輸出輸入範例中,也看不出來。
    高仔,如何呢?

    回覆刪除
  4. 你們誰來暴力點,先列出1~60輸入的結果出來?

    回覆刪除
  5. Dim a(5) As Integer
    Dim L As String
    Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Input #1, x
    Close #1
    a(0) = 40: a(1) = 20: a(2) = 10: a(3) = 5: a(4) = 3
    ans = 0
    For i = 0 To 4
    If x >= a(i) Then x = x - a(i): ans = ans + 1: L = L & a(i) & " "
    If i = 4 And x <> 0 Then ans = ans + 1: L = L & a(i)
    Next i
    Open App.Path & "/out.txt" For Output As #2
    Print #2, "(" & ans & ")" & " : " & L
    Close #2
    End Sub

    回覆刪除
  6. 1 = (1) : 3
    2 = (1) : 3
    3 = (1) : 3
    4 = (2) : 3 3
    5 = (1) : 5
    6 = (2) : 5 3
    7 = (2) : 5 3
    8 = (2) : 5 3
    9 = (3) : 5 3 3
    10 = (1) : 10
    11 = (2) : 10 3
    12 = (2) : 10 3
    13 = (2) : 10 3
    14 = (3) : 10 3 3
    15 = (2) : 10 5
    16 = (3) : 10 5 3
    17 = (3) : 10 5 3
    18 = (3) : 10 5 3
    19 = (4) : 10 5 3 3
    20 = (1) : 20
    21 = (2) : 20 3
    22 = (2) : 20 3
    23 = (2) : 20 3
    24 = (3) : 20 3 3
    25 = (2) : 20 5
    26 = (3) : 20 5 3
    27 = (3) : 20 5 3
    28 = (3) : 20 5 3
    29 = (4) : 20 5 3 3
    30 = (2) : 20 10
    31 = (3) : 20 10 3
    32 = (3) : 20 10 3
    33 = (3) : 20 10 3
    34 = (4) : 20 10 3 3
    35 = (3) : 20 10 5
    36 = (4) : 20 10 5 3
    37 = (4) : 20 10 5 3
    38 = (4) : 20 10 5 3
    39 = (5) : 20 10 5 3 3
    40 = (1) : 40
    41 = (2) : 40 3
    42 = (2) : 40 3
    43 = (2) : 40 3
    44 = (3) : 40 3 3
    45 = (2) : 40 5
    46 = (3) : 40 5 3
    47 = (3) : 40 5 3
    48 = (3) : 40 5 3
    49 = (4) : 40 5 3 3
    50 = (2) : 40 10
    51 = (3) : 40 10 3
    52 = (3) : 40 10 3
    53 = (3) : 40 10 3
    54 = (4) : 40 10 3 3
    55 = (3) : 40 10 5
    56 = (4) : 40 10 5 3
    57 = (4) : 40 10 5 3
    58 = (4) : 40 10 5 3
    59 = (5) : 40 10 5 3 3
    60 = (2) : 40 20

    回覆刪除
  7. 高仔好,
    1、我就是想知道你們「覺得」的答案是多少,才會知道你們的程式想法。
    你列出來的答案中,
    4
    9
    那,我之前寫的,可能的兩組答案呢?
    還有9以 (3) 3 3 3不是更小嗎?
    當然,其它的29 39也類似吧。
    2、所以,你從題本中,也看不出,我提出的「題意不清」的地方囉?

    回覆刪除
  8. x→攜帶的容器,(總)容量最小(x)

    9以 (3) 3 3 3→這個...才是正確的

    x+y+z最小之組合,若有相同者,均需要列出。→突然想到這個意思是其中有一組以上 有2個以上的解答
    所以這題程式碼還得要再改進...ˊˋ

    回覆刪除
  9. 大家好,
    如果如高仔說的,是(總)容量最小的話,那麼,你們都要重寫這題了。
    再來一次吧。

    回覆刪除
  10. 請大家再重新先用「手算」1~11吧。列出來,各自手算的結果吧。

    回覆刪除
  11. Dim va(5) As Integer
    Dim vv(5) As Integer
    Dim str As String
    Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Input #1, x
    Do Until x < 3
    Select Case x
    Case Is >= 40
    x = x - 40
    va(5) = va(5) + 1
    Case Is >= 20
    If x < 40 Then
    x = x - 20
    va(4) = va(4) + 1
    End If
    Case Is >= 10
    If x < 20 Then
    x = x - 10
    va(3) = va(3) + 1
    End If
    Case Is >= 5
    If x < 10 Then
    x = x - 5
    va(2) = va(2) + 1
    End If
    Case Is < 5
    If x >= 3 Then
    x = x - 3
    va(1) = va(1) + 1
    Else
    va(1) = va(1) + 1
    Exit Do
    End If
    End Select
    Loop
    vv(5) = 40
    vv(4) = 20
    vv(3) = 10
    vv(2) = 5
    vv(1) = 3
    For i = 5 To 1 Step -1
    sumV = sumV + va(i)
    If va(i) <> 0 Then
    str = str & " " & vv(i)
    End If
    Next i
    Close #1
    Open App.Path & "/out.txt" For Output As #1
    Print #2, "(" & sumV & "):" & str
    Close #1
    End Sub

    回覆刪除
  12. 小白好,
    程式還算ok,但是,你還得再去了解一下select case 的執行方式。
    如果第一個case 成立,就不會做下一個case了。所以你作了好多個if是不需要的。(?)
    但是,你一樣沒有解決 9 是 3 3 3 的答案吧。

    回覆刪除
  13. Dim a(5) As Integer
    Dim ans As String
    Public Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a(1) = 3: a(2) = 5: a(3) = 10: a(4) = 20: a(5) = 40
    Input #1, x
    chec = 99: min = 0: n = 0
    Call check(x, n, min, chec, "")
    Print #2, ans
    Close #2
    Close #1
    End
    End Sub

    Public Function check(ByVal x, ByVal n, ByVal min, ByVal chec, ByVal st) As Integer
    If (min + n + x) <= chec Then
    chec = min + n + x
    ans = "(" & n & ")" & st
    End If
    For i = 1 To 5
    min = 99
    If a(i) <= min Then min = a(i)
    If x - a(i) > -3 Then Call check(x - a(i), n + 1, min, chec, st & " " & a(i))

    Next i
    End Function

    39分 0.0

    回覆刪除
  14. Dim a(5) As Integer
    Dim ans(9999) As String
    Dim an(9999) As Integer
    Dim num As Integer
    Dim nn As Integer
    Public Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a(1) = 3: a(2) = 5: a(3) = 10: a(4) = 20: a(5) = 40
    Input #1, x
    chec = 99: min = 0: n = 0: num = 1: nn = 99
    Call check(x, n, min, chec, "")
    For i = 1 To num
    If an(i) = nn Then
    Print #2, ans(i)
    End If
    Next i
    Close #2
    Close #1
    End
    End Sub

    Public Function check(ByVal x, ByVal n, ByVal min, ByVal chec, ByVal st) As Integer
    If x < 0 Then x = 0
    If (min + n + x) <= chec Then
    chec = min + n + x
    ans(num) = "(" & n & ")" & st
    an(num) = chec
    If nn > chec Then nn = chec
    num = num + 1
    End If
    For i = 1 To 5
    min = 99
    If a(i) <= min Then min = a(i)
    If x - a(i) > -3 Then Call check(x - a(i), n + 1, min, chec, st & " " & a(i))
    Next i
    End Function

    這有顯示 所有可能

    回覆刪除