Private Sub Form_Load() k = "左盤" w = "右盤" g = "公克" p = 0: o = 0 Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, q Close #1 Dim a(27) As Integer a(1) = q Mod 3: q = q \ 3 a(3) = q Mod 3: q = q \ 3 a(9) = q Mod 3: q = q \ 3 a(27) = q Mod 3: q = q \ 3 If a(1) = 2 Then a(3) = a(3) + 1 If a(3) = 2 Then a(9) = a(9) + 1 If a(3) = 3 Then a(9) = a(9) + 1: a(3) = 0 If a(9) = 2 Then a(27) = a(27) + 1 If a(9) = 3 Then a(27) = a(27) + 1: a(9) = 0 For i = 1 To 27 If a(i) = 1 Then p = p + i Next i For i = 1 To 27 If a(i) = 2 Then o = o + i Next i Print #2, k & p & g, w & o & g Close #2 End Sub
Dim F(5) As Integer Dim Sum(5) As Integer Private Sub Form_Load() Open App.Path & "/in.txt" For Input As #1 Input #1, w Close #1 R = 0: L = 0 Open App.Path & "/out.txt" For Output As #2 If w < 1 Or w > 40 Then Print "輸入錯誤": Exit Sub F(1) = 1: F(2) = 3: F(3) = 9: F(4) = 27 Sum(1) = 1: Sum(2) = 4: Sum(3) = 13: Sum(4) = 40 For i = 1 To 4 If w < Sum(i) Then MaxI = i: Exit For If w = Sum(i) Then R = Sum(i): L = 0: GoTo Ans Next i For i = MaxI To 1 Step -1 If Sum(i - 1) = w Then R = R + a GoTo Ans Else a = w - F(i) End If R = R + F(i) If a < 0 Then a = -(a): change L, R w = w - F(i) If w < 0 Then w = -(w) Next i Ans: Print #2, "左盤" & L & "公克,右盤" & R & "公克" Close #2 End Sub
Dim a(27) As Integer Dim x 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 Input #1, x k = 1 Do Until x = 0 a(k) = x Mod 3 x = x \ 3 k = k * 3 Loop k = 1 Do If a(k) >= 2 Then a(k * 3) = a(k * 3) + 1 If a(k) = 2 Then anl = anl + k If a(k) = 1 Then anr = anr + k k = k * 3 Loop Until k > 27 Print #2, "左盤" & anl & " " & "右盤" & anr Close #2 Close #1 End End Sub
進入了數學題。。。
回覆刪除這個想要簡短
感覺就是殺腦= ="
這種題目要是考試時出來,
回覆刪除就「暴力」些,先將1到40全部手算出來,再寫進程式就行了。
最快。
但是,最「無恥」,哈。
請問 所有法碼 都只有1個嗎???
回覆刪除這樣有些數字 算不出來耶@@
設物品都放右邊 則
回覆刪除物品重 左盤 右盤
1 1 0
2 3 1
3 3 0
4 3+1 0
5 9 3+1
6 9 3
7 9+1 3
8 9 1
9 9 0
10 9+1 0
11 9+3 1
12 9+3 0
13 9+3+1 0
14 27 9+3+1
15 27 9+3
16 27+1 9+3
17 27 9+1
18 27 9
19 27+1 9
20 27+3 9+1
21 27+3 9
22 27+3+1 9
23 27 3+1
24 27 3
25 27+1 3
26 27 1
27 27 0
以此類推
參考一下吧
我看錯了@@
回覆刪除1~40 好像 全部都有 答案....
因為平時空閒時間多
回覆刪除不像考試緊迫
所以倒是把暴力方法都給捨棄了(會很粗糙= =")
目前的想法是有
法碼有1、3、9、27
1跟3之間隔為1 若物品為1或3就直接答案 為2則是左3右1抵銷
4(1+3)跟9之間隔為4
13(1+3+9)跟27之間隔為13
雖然有些麻煩
但是還是可以細細分解
只是想法是一回事
實行起來還是頗為無力
我有1個想法。
回覆刪除就是 把它想成 4進位
剛剛 提供的資料 我發現好像可以用這個方法@@
單從這4個來看 4進位 分別是
1 0001 左邊1個(1) 右邊沒有
2 0002 左邊1個(3) 右邊1個(1)
3 0010 左邊1個(3) 右邊沒有
4 0011 左邊1個(3) 左邊1個(1)
有沒有發現其中的規律呢?
只要4進位 1就是 左邊 2的話就是右邊 但是 2的話又會再多
出1法碼 比如說 2 0002 卻是 1個 (3) 1個(1)
這只是 我的推測=ˋ=希望有幫助 :D
Private Sub Form_Load()
回覆刪除k = "左盤"
w = "右盤"
g = "公克"
p = 0: o = 0
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, q
Close #1
Dim a(27) As Integer
a(1) = q Mod 3: q = q \ 3
a(3) = q Mod 3: q = q \ 3
a(9) = q Mod 3: q = q \ 3
a(27) = q Mod 3: q = q \ 3
If a(1) = 2 Then a(3) = a(3) + 1
If a(3) = 2 Then a(9) = a(9) + 1
If a(3) = 3 Then a(9) = a(9) + 1: a(3) = 0
If a(9) = 2 Then a(27) = a(27) + 1
If a(9) = 3 Then a(27) = a(27) + 1: a(9) = 0
For i = 1 To 27
If a(i) = 1 Then p = p + i
Next i
For i = 1 To 27
If a(i) = 2 Then o = o + i
Next i
Print #2, k & p & g, w & o & g
Close #2
End Sub
輸入 33 輸出 左盤36公克 右盤3公克
輸入 40 輸出 左盤40公克 右盤0公克
Y揚好,
回覆刪除你做得很好,程式應該是對的。
只錯了一個計概的小問題:
那個不叫4進位,是叫__進位?
3進位@@
回覆刪除\
Dim F(5) As Integer
回覆刪除Dim Sum(5) As Integer
Private Sub Form_Load()
Open App.Path & "/in.txt" For Input As #1
Input #1, w
Close #1
R = 0: L = 0
Open App.Path & "/out.txt" For Output As #2
If w < 1 Or w > 40 Then Print "輸入錯誤": Exit Sub
F(1) = 1: F(2) = 3: F(3) = 9: F(4) = 27
Sum(1) = 1: Sum(2) = 4: Sum(3) = 13: Sum(4) = 40
For i = 1 To 4
If w < Sum(i) Then MaxI = i: Exit For
If w = Sum(i) Then R = Sum(i): L = 0: GoTo Ans
Next i
For i = MaxI To 1 Step -1
If Sum(i - 1) = w Then
R = R + a
GoTo Ans
Else
a = w - F(i)
End If
R = R + F(i)
If a < 0 Then a = -(a): change L, R
w = w - F(i)
If w < 0 Then w = -(w)
Next i
Ans:
Print #2, "左盤" & L & "公克,右盤" & R & "公克"
Close #2
End Sub
Public Sub change(a, b)
c = a
a = b
b = c
End Sub
用到change有點輾轉的感覺
雖然很殺腦又耗時間
不過終於把我所想的方法表達出來了
題目中沒規定 物品的位置
所以 我這個小缺點:物品位置不一定
應該也就不太會造成失分了
這題感覺就是挑戰數學
完成了好感動 >"<
高仔好,
回覆刪除1.你的程式是正確的,想出程式的感覺,是蠻好的,這是我們這些程式人的共同感動。恭喜你。
2.這題更正統解法,還是從3進位數來解。
3^0 =1
3^1 =3
3^2 =9
3^3 =27
如果每個法碼各有2個,就是這麼單純的問題。
3.只是這題中的法碼,各只有1個。於是,需要2個相同的法碼時,就改成1個大1號的法碼,減去1個同號的法碼。
4.所以,因為數字系統,正整數的部份,是可以全部表示的,每個數字都行。
用這樣正統的解法的話,如果加1個3^4=81克的法碼,就可以表示1~121的所有重量了。
5.所以,Y揚的方式,最好。
6.這樣的方式想了一次後,以後,不管n進位都相同了吧。4進位,5進位,6進位,7進位,8進位。不過,法碼的個數,至少都是n-2個吧。
Dim a(27) As Integer
回覆刪除Dim x 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
Input #1, x
k = 1
Do Until x = 0
a(k) = x Mod 3
x = x \ 3
k = k * 3
Loop
k = 1
Do
If a(k) >= 2 Then a(k * 3) = a(k * 3) + 1
If a(k) = 2 Then anl = anl + k
If a(k) = 1 Then anr = anr + k
k = k * 3
Loop Until k > 27
Print #2, "左盤" & anl & " " & "右盤" & anr
Close #2
Close #1
End
End Sub
18分29秒