天天看点

走阶梯算法设计(源码)

 要求: 某人上阶梯,阶梯共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

继续阅读