2011年5月30日 星期一

天平秤重

有一天平,共有四個法碼,分別為1、3、9、27公克。請設計一程式,由"in.txt"輸入某一物品質量(1~40公克)。


輸入範例:
8公克


輸出範例:
左盤1公克,右盤9公克



4 則留言:

  1. Dim i1, i3, i9, i27, N, R As Integer, L 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

    Input #1, N


    Call Func1(N)

    Close
    Close
    End
    End Sub

    Sub Func1(a)
    For i1 = -1 To 1
    For i3 = -1 To 1
    For i9 = -1 To 1
    For i27 = -1 To 1
    If 1 * i1 + 3 * i3 + 9 * i9 + 27 * i27 = a Then Call func2(i1, i3, i9, i27)
    Next
    Next
    Next
    Next
    End Sub

    Sub func2(a, b, c, d)

    If a > 0 Then R = R + 1
    If a < 0 Then L = L + 1
    If b > 0 Then R = R + 3
    If b < 0 Then L = L + 3
    If c > 0 Then R = R + 9
    If c < 0 Then L = L + 9
    If d > 0 Then R = R + 27
    If d < 0 Then L = L + 27
    Print #2, " 左盤" & L & "公克,右盤" & R & "公克"
    End Sub
    /////////////////////////////
    輸入:9公克
    輸出: 左盤0公克,右盤9公克

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Do While Not EOF(1)
    Input #1, n
    x = Split("-1 +0 +1")
    Call c("", UBound(x), x)
    For I = 0 To List1.ListCount - 1
    r = Split(Right(List1.List(I), Len(List1.List(I)) - 1))
    r(0) = 1 * Val(r(0))
    r(1) = 3 * Val(r(1))
    r(2) = 9 * Val(r(2))
    r(3) = 27 * Val(r(3))
    ANS = Val(r(0)) + Val(r(1)) + Val(r(2)) + Val(r(3))
    If n = ANS Then
    For j = 0 To 3
    Select Case Left(r(j), 1)
    Case "1": a = a + Val(r(j))
    Case "3": a = a + Val(r(j))
    Case "9": a = a + Val(r(j))
    Case "2": a = a + Val(r(j))
    Case "-": b = b + Val(Right(r(j), 1))
    End Select
    Next j
    Print #2, "左盤 " & Val(a) & " 公克;右盤 " & Val(b) & " 公克"
    GoTo ne
    End If
    Next
    ne:
    a = 0: b = 0
    Loop
    Close #1
    Close #2
    End
    End Sub
    Sub c(a1, b1, c1)
    If Len(a1) = 12 Then
    List1.AddItem a1
    Else
    For I = 0 To b1
    Call c(a1 & " " & c1(I), b1, c1)
    Next
    End If
    End Sub
    --------------------
    in.txt
    10
    20
    22
    12
    29
    --------------------
    out.txt
    左盤 10 公克;右盤 0 公克
    左盤 30 公克;右盤 10 公克
    左盤 31 公克;右盤 9 公克
    左盤 12 公克;右盤 0 公克
    左盤 30 公克;右盤 1 公克

    回覆刪除
  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
    Input #1, N
    VR = Val(Left(N, 1))

    For A = -1 To 1
    For B = -1 To 1
    For C = -1 To 1
    For D = -1 To 1
    If VR = A + B * 3 + C * 9 + D * 27 Then Call ABC(A, B, C, D)
    Next
    Next
    Next
    Next

    Close #2
    Close #1


    End
    End Sub

    Sub ABC(A, B, C, D)
    L = 0: R = 0
    If A = -1 Then L = L + 1
    If A = 1 Then R = R + 1
    If B = -1 Then L = L + 3
    If B = 1 Then R = R + 3
    If C = -1 Then L = L + 9
    If C = 1 Then R = R + 9
    If D = -1 Then L = L + 27
    If D = 1 Then R = R + 27
    Print #2, "左盤" & L & "公克,右盤" & R & "公克"
    End Sub


    輸入範例:
    10公克


    輸出範例:
    左盤0公克,右盤10公克

    10為(9+1)

    回覆刪除
  4. arro,緣尉,佑好,
    程式應該都正確,緣尉的程式,對於內容有太多的不確定感,不好。
    字串和數字之間,在變數裡或是在陣列裡,都要清楚它應該是什麼。
    雖然強制用val來轉換,但是,不確定感,太危險。

    回覆刪除