问题描述: 抬起一堆硬币。翻转最顶部的硬币,然后继续:将前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
提前致谢
此致
答案 0 :(得分:3)
在For pass = 1 To passes
循环中,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 finished
... Loop
构造答案 1 :(得分:0)
Sub Flip()
Dim rw As Range
Dim numCoins As Integer
Dim iCoins As Integer, iCoin As Integer, flipCoins As Integer
Dim v
numCoins = 6
Set rw = Sheet1.Range("B2").Resize(1, numCoins) 'all start as "TRUE"
rw.Value = True
Do
For flipCoins = 1 To numCoins
For iCoin = 1 To numCoins
If iCoin <= flipCoins Then
v = Not rw.Cells(flipCoins - (iCoin - 1)).Value
Else
v = rw.Cells(iCoin).Value
End If
rw.Offset(1, 0).Cells(iCoin).Value = v
Next iCoin
Set rw = rw.Offset(1, 0)
rw.EntireRow.Cells(1).Value = "Flipped " & flipCoins
If Application.CountIf(rw, "FALSE") = 0 Then
Debug.Print "All Heads at row " & rw.Row
Exit Do
End If
Next flipCoins
Loop While rw.Row < 1000 'don't go on for ever...
End Sub
答案 2 :(得分:0)
我怀疑这个
Fst = Fst + 1
Lst = Flip_x_coins - 1
Next pass
应该是
Fst = Fst + 1
Lst = Lst - 1
Next pass