我似乎无法发现错误,逻辑错误?
问题描述: 拿一叠硬币,正面朝上。将最上面的硬币翻转,然后继续:取出最上面的 2 个硬币并翻转为单个堆栈(当翻转并放回堆栈时,尾部、头部变成尾部、头部(两枚硬币翻转,就像粘在一起一样))。现在以同样的方式翻转顶部 3 个硬币并放回堆栈上(您得到:尾部、尾部、头(如果有 4 个硬币,则将是尾部、尾部、尾部、头)。当您翻转整个堆栈时再次从第一个硬币开始。继续,直到您返回到所有抬头的堆栈
(希望这是清楚的)
有人能明白为什么这个小程序会失败吗?我第一次注意到错误的例子是当计数达到 18 时。 一叠 6 枚硬币。
我放了 电子表格上的按钮并调用 FlippingCoins...
Sub FlippingCoins()
Call theStackOfCoins
Call theFlipping
End Sub
Sub theStackOfCoins()
Worksheets("Sheet3").Cells(1, 3).Select
Columns("A:b").Select
Selection.ClearContents
Range("a3").Select
Dim StackOfCoins As Integer
StackOfCoins = Worksheets("Sheet3").Cells(1, 3).Value
Dim row As Integer
row = 0
For theStack = 1 To StackOfCoins
Worksheets("Sheet3").Cells(row + theStack, 1).Value = True
Next theStack
End Sub
Sub theFlipping()
Dim middleCoin As Integer
middleCoin = 0
Dim passes As Integer
passes = 0
Dim Fst As Integer
Fst = 0
Dim Lst As Integer
Lst = 0
Dim stack As Integer
stack = Worksheets("Sheet3").Cells(1, 3).Value
Dim Flip_x_coins As Integer
Flip_x_coins = 0
Dim count As Integer
count = 0
Dim Finished As Boolean
Finished = False
Reset:
Flip_x_coins = 1
For Flip_x_coins = 1 To stack
Worksheets("Sheet3").Cells(1, 4).Value = Flip_x_coins
count = count + 1
If Flip_x_coins = 1 Then
Worksheets("Sheet3").Cells(1, 1).Value = Not (Worksheets("Sheet3").Cells(1, 1).Value)
Else
passes = Int(Flip_x_coins) / 2
Fst = 1
Lst = Flip_x_coins
For pass = 1 To passes
If Worksheets("Sheet3").Cells(Fst, 1).Value = Worksheets("Sheet3").Cells(Lst, 1).Value Then
Worksheets("Sheet3").Cells(Fst, 1).Value = Not (Worksheets("Sheet3").Cells(Fst, 1).Value)
Worksheets("Sheet3").Cells(Lst, 1).Value = Not (Worksheets("Sheet3").Cells(Lst, 1).Value)
End If
Fst = Fst + 1
Lst = Flip_x_coins - 1
Next pass
If Flip_x_coins Mod 2 > 0 Then
middleCoin = (Flip_x_coins + 1) / 2
Worksheets("Sheet3").Cells(middleCoin, 1).Value = Not (Worksheets("Sheet3").Cells(middleCoin, 1).Value)
End If
End If
For testComplete = 1 To stack
If Worksheets("Sheet3").Cells(testComplete, 1).Value = False Then
Finished = False
Exit For
Else
Finished = True
End If
Next testComplete
Worksheets("Sheet3").Cells(1, 2).Value = count
If Finished = True Then
Exit For
End If
MsgBox "Next."
If Flip_x_coins = stack Then
GoTo Reset
End If
Next Flip_x_coins
End Sub
提前
致谢
Problem description:
Take a stack of coins all heads up. Upturn the topmost coin and then proceed: take the top 2 coins and upturn as a single stack (tail, head becomes when upturned and placed back on the stack tail, head (the two coins are flipped as if glued together)). Now in the same way flip the top 3 coins and place back on the stack (you get: tail, tail, head (and if there were 4 coins that would be tail, tail, tail, head). When you upturn the whole stack begin again with the first coin. Continue until you return to a stack with all heads up.
(Hope that's clear)
Can anybody see why this small program should fail? The example for me where I first notice an error is when count reaches 18 with a stack of 6 coins.
I placed a button on a spreadsheet and call FlippingCoins...
Sub FlippingCoins()
Call theStackOfCoins
Call theFlipping
End Sub
Sub theStackOfCoins()
Worksheets("Sheet3").Cells(1, 3).Select
Columns("A:b").Select
Selection.ClearContents
Range("a3").Select
Dim StackOfCoins As Integer
StackOfCoins = Worksheets("Sheet3").Cells(1, 3).Value
Dim row As Integer
row = 0
For theStack = 1 To StackOfCoins
Worksheets("Sheet3").Cells(row + theStack, 1).Value = True
Next theStack
End Sub
Sub theFlipping()
Dim middleCoin As Integer
middleCoin = 0
Dim passes As Integer
passes = 0
Dim Fst As Integer
Fst = 0
Dim Lst As Integer
Lst = 0
Dim stack As Integer
stack = Worksheets("Sheet3").Cells(1, 3).Value
Dim Flip_x_coins As Integer
Flip_x_coins = 0
Dim count As Integer
count = 0
Dim Finished As Boolean
Finished = False
Reset:
Flip_x_coins = 1
For Flip_x_coins = 1 To stack
Worksheets("Sheet3").Cells(1, 4).Value = Flip_x_coins
count = count + 1
If Flip_x_coins = 1 Then
Worksheets("Sheet3").Cells(1, 1).Value = Not (Worksheets("Sheet3").Cells(1, 1).Value)
Else
passes = Int(Flip_x_coins) / 2
Fst = 1
Lst = Flip_x_coins
For pass = 1 To passes
If Worksheets("Sheet3").Cells(Fst, 1).Value = Worksheets("Sheet3").Cells(Lst, 1).Value Then
Worksheets("Sheet3").Cells(Fst, 1).Value = Not (Worksheets("Sheet3").Cells(Fst, 1).Value)
Worksheets("Sheet3").Cells(Lst, 1).Value = Not (Worksheets("Sheet3").Cells(Lst, 1).Value)
End If
Fst = Fst + 1
Lst = Flip_x_coins - 1
Next pass
If Flip_x_coins Mod 2 > 0 Then
middleCoin = (Flip_x_coins + 1) / 2
Worksheets("Sheet3").Cells(middleCoin, 1).Value = Not (Worksheets("Sheet3").Cells(middleCoin, 1).Value)
End If
End If
For testComplete = 1 To stack
If Worksheets("Sheet3").Cells(testComplete, 1).Value = False Then
Finished = False
Exit For
Else
Finished = True
End If
Next testComplete
Worksheets("Sheet3").Cells(1, 2).Value = count
If Finished = True Then
Exit For
End If
MsgBox "Next."
If Flip_x_coins = stack Then
GoTo Reset
End If
Next Flip_x_coins
End Sub
Thanks in advance
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
For pass = 1 To Passs
循环中,Lst = Flip_x_coins - 1
是错误的。它应该是:
Lst = Lst - 1
在第 18 遍有 6 个硬币时,宏比较第 1 行和第 6 行,然后比较第 2 行和第 5 行,然后比较第 3 行和第 5 行。显然,最后一次比较应该在改为第 3 行和第 4 行。
我希望这不是家庭作业,因为宏还有很多其他问题。例如:
Option Explicit
。这允许您使用三个未声明的变量 -theStack
、pass
、testComplete
Flip_x_coins
是Integer
类型,passes = Int(Flip_x_coins) / 2
是无意义的。尝试使用passes = Int(Flip_x_coins / 2)
而不是Goto
通常是一个坏主意。它在 VBA 中用于错误处理,但在这种情况下,您可以轻松地使用Do Until finish
...Loop
构造来代替In the
For pass = 1 To passes
loop,Lst = Flip_x_coins - 1
is wrong.It should be:
Lst = Lst - 1
On pass 18 with 6 coins, the macro compares rows 1 and 6 followed by rows 2 and 5 followed by rows 3 and 5. Obviously the last comparison should be between rows 3 and 4 instead.
I hope this isn't homework because there are lots of other problems with the macro. For example:
Option Explicit
at the start of the macro. This has allowed you to use three variables which you haven't declared -theStack
,pass
,testComplete
Flip_x_coins
is ofInteger
type,passes = Int(Flip_x_coins) / 2
is nonsense. Trypasses = Int(Flip_x_coins / 2)
insteadGoto
is generally a bad idea. It has some use in VBA for error handling but, in this case, you could easily use aDo Until finished
...Loop
construct instead我怀疑这
应该是
I suspect this
should be