我似乎无法发现错误,逻辑错误?

发布于 2024-12-03 12:54:24 字数 2648 浏览 2 评论 0原文

问题描述: 拿一叠硬币,正面朝上。将最上面的硬币翻转,然后继续:取出最上面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

预谋 2024-12-10 12:54:24

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。这允许您使用三个未声明的变量 - theStackpasstestComplete
  • 不正确的舍入。鉴于 Flip_x_coinsInteger 类型,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:

  • 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 of Integer type, passes = Int(Flip_x_coins) / 2 is nonsense. Try passes = 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 a Do Until finished ... Loop construct instead
长亭外,古道边 2024-12-10 12:54:24

我怀疑这

            Fst = Fst + 1
            Lst = Flip_x_coins - 1
        Next pass

应该是

            Fst = Fst + 1
            Lst = Lst - 1
        Next pass

I suspect this

            Fst = Fst + 1
            Lst = Flip_x_coins - 1
        Next pass

should be

            Fst = Fst + 1
            Lst = Lst - 1
        Next pass
无所的.畏惧 2024-12-10 12:54:24
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文