天天看點

走階梯算法設計(源碼)

 要求: 某人上階梯,階梯共100道,可以一次跨1個階梯,可以一次跨2個階梯,可以一次跨3個階梯,問上一次階梯共有多少種走法?另請列出每種走法的步數排列(按先後順序)!

'高精度正整數加減法,即大正整數加減法

'減法運算,已知第一數大于第二數

'主要用于本程式,為簡化代碼,不做轉換和符号判斷,如果想實作通用運算,請另外調整

Function dsys(n1, n2, f) As String

    Dim b1(), b2(), c, i, s

    '取大數長度分段配置設定數組大小

    c = IIf(Len(n1) > Len(n2), Int(Len(n1) / 12), Int(Len(n2) / 12)) + 1

    ReDim b1(c + 1), b2(c)

    s = 0

    'n1按12位分段取數存入數組

    For i = 1 To Len(n1) Step 12

        s = s + 1

        If Len(n1) > s * 12 Then

            b1(s) = Mid(n1, Len(n1) - s * 12 + 1, 12)

        Else

            b1(s) = Mid(n1, 1, Len(n1) - (s - 1) * 12)

        End If

    Next i

    'n2按12位分段取數存入數組

    s = 0

    For i = 1 To Len(n2) Step 12

        s = s + 1

        If Len(n2) > s * 12 Then

            b2(s) = Mid(n2, Len(n2) - s * 12 + 1, 12)

        Else

            b2(s) = Mid(n2, 1, Len(n2) - (s - 1) * 12)

        End If

    Next i

    '加法

    If f = 1 Then

        '按段相加,結果存入數組

        For i = 1 To c

            b1(i) = Val(b1(i)) + Val(b2(i))

            '進位

            If Len(b1(i)) > 12 Then

                b1(i + 1) = Val(b1(i + 1)) + Val(Left(b1(i), Len(b1(i)) - 12))

                b1(i) = Right(b1(i), 12)

            End If

            '不足12位補0

            dsys = String(12 - Len(b1(i)), "0") & b1(i) & dsys

        Next i

    '減法

    Else

        '按段相減,結果存入數組

        For i = 1 To c

            b1(i) = Val(b1(i)) - Val(b2(i))

            '借位

            If b1(i) < 0 Then

                b1(i + 1) = Val(b1(i + 1)) - 1

                b1(i) = Val(b1(i)) + 10 ^ 12

            End If

            '不足12位補0

            dsys = String(12 - Len(b1(i)), "0") & b1(i) & dsys

        Next i

    End If

    '去前0

    For i = 1 To Len(dsys)

        If Mid(dsys, i, 1) <> "0" Then

            dsys = Mid(dsys, i)

            Exit For

        End If

    Next i

End Function

'階梯走法算法

Function dgzf(t As Integer, m As String) As String

Dim i As Integer

    '遞歸到1階的走法

    If t = 1 Then

        dgzf = "1"

        Exit Function

    '遞歸到2階的第2種走法

    ElseIf t = 2 And m = 2 Then

        dgzf = "2"

        Exit Function

    '遞歸到3階的第4種走法

    ElseIf t = 3 And m = 4 Then

        dgzf = "3"

        Exit Function

    '假如走法數小等于上一階走法總數

    ElseIf Not (dsbj(m, a(t - 1))) Then

        dgzf = "1" & dgzf(t - 1, m)

    '假如走法數小等于上兩階走法總數

    ElseIf dsbj(m, a(t - 1)) And Not (dsbj(m, dsys(a(t - 1), a(t - 2), 1))) Then

        dgzf = "2" & dgzf(t - 2, dsys(m, a(t - 1), 2))

    '假如走法數大于上兩階走法總數

    ElseIf dsbj(m, dsys(a(t - 1), a(t - 2), 1)) Then

        dgzf = "3" & dgzf(t - 3, dsys(dsys(m, a(t - 1), 2), a(t - 2), 2))

    End If

End Function

以下為程式源碼,将以下内容複制粘貼到建立的文本檔案中,儲存後再将檔案字尾名改名 .frm

VERSION 5.00

Begin VB.Form Form1

   Caption         =   "走階梯算法設計"

   ClientHeight    =   4920

   ClientLeft      =   60

   ClientTop       =   345

   ClientWidth     =   6360

   LinkTopic       =   "Form1"

   ScaleHeight     =   4920

   ScaleWidth      =   6360

   StartUpPosition =   3  '視窗預設

   Begin VB.Frame Frame2

      Caption         =   "求解:"

      ForeColor       =   &H000000FF&

      Height          =   3735

      Left            =   120

      TabIndex        =   2

      Top             =   1080

      Width           =   6135

      Begin VB.CommandButton Command2

         Caption         =   "結果清空"

         Height          =   375

         Left            =   4560

         TabIndex        =   11

         Top             =   240

         Width           =   1215

      End

      Begin VB.TextBox Text3

         Height          =   270

         Left            =   1080

         TabIndex        =   10

         Top             =   1155

         Width           =   4935

      End

      Begin VB.CommandButton Command1

         Caption         =   "求解走法"

         Default         =   -1  'True

         Height          =   375

         Left            =   3000

         TabIndex        =   9

         Top             =   240

         Width           =   1215

      End

      Begin VB.TextBox Text1

         Height          =   2055

         Left            =   120

         MultiLine       =   -1  'True

         ScrollBars      =   2  'Vertical

         TabIndex        =   8

         Top             =   1560

         Width           =   5895

      End

      Begin VB.ComboBox Combo1

         Height          =   300

         Left            =   1680

         Style           =   2  'Dropdown List

         TabIndex        =   6

         Top             =   270

         Width           =   975

      End

      Begin VB.TextBox Text2

         Height          =   270

         Left            =   1080

         Locked          =   -1  'True

         TabIndex        =   5

         Top             =   720

         Width           =   4935

      End

      Begin VB.Label Label4

         AutoSize        =   -1  'True

         Caption         =   "某種走法:"

         Height          =   180

         Left            =   120

         TabIndex        =   7

         Top             =   1200

         Width           =   900

      End

      Begin VB.Label Label3

         AutoSize        =   -1  'True

         Caption         =   "走法總數:"

         Height          =   180

         Left            =   120

         TabIndex        =   4

         Top             =   765

         Width           =   900

      End

      Begin VB.Label Label1

         AutoSize        =   -1  'True

         Caption         =   "階梯數(1-200):"

         Height          =   180

         Left            =   120

         TabIndex        =   3

         Top             =   330

         Width           =   1350

      End

   End

   Begin VB.Frame Frame1

      BackColor       =   &H80000016&

      Caption         =   "要求:"

      ForeColor       =   &H000000FF&

      Height          =   975

      Left            =   120

      TabIndex        =   0

      Top             =   0

      Width           =   6135

      Begin VB.Label Label2

         BackColor       =   &H80000016&

         BackStyle       =   0  'Transparent

         Caption         =   $"Form1.frx":0000

         ForeColor       =   &H00FF0000&

         Height          =   615

         Left            =   240

         TabIndex        =   1

         Top             =   240

         Width           =   5775

      End

   End

End

Attribute VB_Name = "Form1"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Option Explicit

' 要求: 某人上階梯,階梯共100道,可以一次跨1個階梯,可以一次跨2個階梯,可以一次跨3個階梯,問

'       上一次階梯共有多少種走法?另請列出每種走法的步數排列(按先後順序)!

Dim a() As String

Private Sub Command1_Click()

Dim str1 As String

    If dsbj(Text3.Text, a(Combo1.Text)) Or Text3.Text = "" Or Text3 Like "*[!0-9]*" Then

        MsgBox "輸入資料過大或非數字", vbOKOnly + vbExclamation, "階梯算法"

        Text3.SetFocus

        Exit Sub

    End If

    str1 = dgzf(Combo1.Text, Text3.Text)

    Text1 = vbNewLine & Combo1.Text & "級階梯第【" & Text3 & "】種走法:" & vbNewLine & str1 & vbNewLine & Text1

End Sub

Private Sub Combo1_Click()

    Text2 = a(Combo1.Text)

    Text2.ToolTipText = Text2

End Sub

Private Sub Command2_Click()

    Text1 = ""

End Sub

Private Sub Text2_Change()

    If dsbj(Text3, Text2) Or Text3 = "" Then Text3 = Text2

End Sub

'輸入資料合法性判斷

Private Sub Text3_KeyPress(KeyAscii As Integer)

Dim str1 As String

    If KeyAscii = vbKeyRight Or KeyAscii = vbKeyLeft Or KeyAscii = vbKeyBack Then

    ElseIf KeyAscii < vbKey0 Or KeyAscii > vbKey9 Then

        KeyAscii = 0

    Else

        str1 = Text3

        str1 = Left(str1, Text3.SelStart) & Chr(KeyAscii) & Mid(str1, Text3.SelStart + Text3.SelLength + 1)

        If dsbj(str1, Text2) Or str1 Like "0*" Then KeyAscii = 0

    End If

End Sub

Private Sub Form_Load()

Dim i As Integer

    zfzs 200

    For i = 1 To 200

        Combo1.AddItem i

    Next i

    Combo1.Text = 100

End Sub

'階梯走法算法

Function dgzf(t As Integer, m As String) As String

Dim i As Integer

    '遞歸到1階的走法

    If t = 1 Then

        dgzf = "1"

        Exit Function

    '遞歸到2階的第2種走法

    ElseIf t = 2 And m = 2 Then

        dgzf = "2"

        Exit Function

    '遞歸到3階的第4種走法

    ElseIf t = 3 And m = 4 Then

        dgzf = "3"

        Exit Function

    '假如走法數小等于上一階走法總數

    ElseIf Not (dsbj(m, a(t - 1))) Then

        dgzf = "1" & dgzf(t - 1, m)

    '假如走法數小等于上兩階走法總數

    ElseIf dsbj(m, a(t - 1)) And Not (dsbj(m, dsys(a(t - 1), a(t - 2), 1))) Then

        dgzf = "2" & dgzf(t - 2, dsys(m, a(t - 1), 2))

    '假如走法數大于上兩階走法總數

    ElseIf dsbj(m, dsys(a(t - 1), a(t - 2), 1)) Then

        dgzf = "3" & dgzf(t - 3, dsys(dsys(m, a(t - 1), 2), a(t - 2), 2))

    End If

End Function

'求某一階數的走法總數,f(n)=f(n-1)+f(n-2)+f(n-3)

Function zfzs(n As Integer) As String

    ReDim a(n)

    Dim i As Integer

    If n > 0 Then a(1) = 1

    If n > 1 Then a(2) = 2

    If n > 2 Then a(3) = 4

    For i = 4 To n

        If Len(a(i - 1)) < 13 Then

            a(i) = Val(a(i - 1)) + Val(a(i - 2)) + Val(a(i - 3))

        Else

            a(i) = dsys(dsys(a(i - 1), a(i - 2), 1), a(i - 3), 1)

        End If

    Next i

    zfzs = a(n)

End Function

'高精度正整數加減法,即大正整數加減法

'減法運算,已知第一數大于第二數

'主要用于本程式,為簡化代碼,不做轉換和符号判斷,如果想實作通用運算,請另外調整

Function dsys(n1, n2, f) As String

    Dim b1(), b2(), c, i, s

    '取大數長度分段配置設定數組大小

    c = IIf(Len(n1) > Len(n2), Int(Len(n1) / 12), Int(Len(n2) / 12)) + 1

    ReDim b1(c + 1), b2(c)

    s = 0

    'n1按12位分段取數存入數組

    For i = 1 To Len(n1) Step 12

        s = s + 1

        If Len(n1) > s * 12 Then

            b1(s) = Mid(n1, Len(n1) - s * 12 + 1, 12)

        Else

            b1(s) = Mid(n1, 1, Len(n1) - (s - 1) * 12)

        End If

    Next i

    'n2按12位分段取數存入數組

    s = 0

    For i = 1 To Len(n2) Step 12

        s = s + 1

        If Len(n2) > s * 12 Then

            b2(s) = Mid(n2, Len(n2) - s * 12 + 1, 12)

        Else

            b2(s) = Mid(n2, 1, Len(n2) - (s - 1) * 12)

        End If

    Next i

    '加法

    If f = 1 Then

        '按段相加,結果存入數組

        For i = 1 To c

            b1(i) = Val(b1(i)) + Val(b2(i))

            '進位

            If Len(b1(i)) > 12 Then

                b1(i + 1) = Val(b1(i + 1)) + Val(Left(b1(i), Len(b1(i)) - 12))

                b1(i) = Right(b1(i), 12)

            End If

            '不足12位補0

            dsys = String(12 - Len(b1(i)), "0") & b1(i) & dsys

        Next i

    '減法

    Else

        '按段相減,結果存入數組

        For i = 1 To c

            b1(i) = Val(b1(i)) - Val(b2(i))

            '借位

            If b1(i) < 0 Then

                b1(i + 1) = Val(b1(i + 1)) - 1

                b1(i) = Val(b1(i)) + 10 ^ 12

            End If

            '不足12位補0

            dsys = String(12 - Len(b1(i)), "0") & b1(i) & dsys

        Next i

    End If

    '去前0

    For i = 1 To Len(dsys)

        If Mid(dsys, i, 1) <> "0" Then

            dsys = Mid(dsys, i)

            Exit For

        End If

    Next i

End Function

'正整數字元串比較大小

Function dsbj(n1 As String, n2 As String) As Boolean

    dsbj = IIf(Len(n1) > Len(n2) Or Len(n1) = Len(n2) And n1 > n2, True, False)

End Function

繼續閱讀