我似乎无法发现的错误,逻辑错误? [英] A bug I can't seem to spot, faulty logic?
问题描述
问题描述: 抬起头来一叠硬币.向上翻转最上面的硬币,然后继续:将最上面的2个硬币向上翻转,作为一个单独的堆叠(尾巴,当翻转时变成头部,然后放回到堆叠的尾部,头部(两个硬币翻转在一起,好像粘在一起)).现在,以同样的方式翻转前3个硬币,然后放回堆栈中(得到:尾巴,尾巴,头部(如果有4个硬币将是尾巴,尾巴,尾巴,头部).从第一个硬币重新开始.继续直到所有头都朝上的堆叠为止.
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.
(希望如此)
有人能看到为什么这个小程序会失败吗?对于我来说,我首先注意到一个错误的示例是当计数达到18时(带6个硬币的堆叠).
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.
我在电子表格上放置了一个按钮,并调用了FlippingCoins ...
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
预先感谢
致谢
推荐答案
在For pass = 1 To passes
循环中,Lst = Flip_x_coins - 1
是错误的.
应为:Lst = Lst - 1
在传递带有6个硬币的第18张时,宏会比较第1行和第6行,然后比较第2行和第5行,然后比较第3行和第5行.显然,最后一次比较应该是在第3行和第4行之间.
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
.这使您可以使用未声明的三个变量-theStack
,pass
,testComplete
- 舍入不正确.假定
Flip_x_coins
是Integer
类型,则passes = Int(Flip_x_coins) / 2
是无意义的.试试passes = Int(Flip_x_coins / 2)
代替 - 使用
Goto
通常不是一个好主意.它在VBA中用于错误处理,但在这种情况下,您可以轻松地使用Do Until finished
...Loop
构造
- no
Option Explicit
at the start of the macro. This has allowed you to use three variables which you haven't declared -theStack
,pass
,testComplete
- incorrect rounding. Given that
Flip_x_coins
is ofInteger
type,passes = Int(Flip_x_coins) / 2
is nonsense. Trypasses = Int(Flip_x_coins / 2)
instead - using
Goto
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
这篇关于我似乎无法发现的错误,逻辑错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!