正文

VB代码 杨辉三角2010-05-14 21:40:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lym51/51135.html

分享到:

VERSION 5.00
Begin VB.Form Form1 
   AutoRedraw      =   -1  'True
   Caption         =   "打印杨辉三角"
   ClientHeight    =   8220
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   10995
   LinkTopic       =   "Form1"
   ScaleHeight     =   8220
   ScaleWidth      =   10995
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Print1 
      Caption         =   "打印杨辉三角"
      Height          =   375
      Left            =   8880
      TabIndex        =   2
      Top             =   120
      Width           =   1215
   End
   Begin VB.TextBox Text 
      Height          =   375
      Left            =   7320
      TabIndex        =   1
      Top             =   120
      Width           =   1335
   End
   Begin VB.Label Label1 
      Caption         =   "输入N:"
      Height          =   255
      Left            =   6120
      TabIndex        =   0
      Top             =   240
      Width           =   735
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False



Private Sub Print1_Click()
    Dim i, j As Integer
    Dim n As Integer
    Dim a(40, 40) As Integer
    Dim str As String
    str = ""
    n = Text.Text
    Print "N的值不大于15!"
    If n <= 0 Or n > 40 Then
        Print "输入的数不合法!请重新输入"
        Text.Text = ""
        Text.SetFocus
    End If
    Print1.SetFocus
    For i = 0 To n             '初始化
        For j = 0 To n + i + 1
            a(i, j) = 0
        Next
        a(i, n - i) = 1
        a(i, n + i) = 1
    Next
    For i = 2 To n
        For j = 1 To n + i
            a(i, j) = a(i - 1, j - 1) + a(i - 1, j + 1)
        Next
    Next
    
    Print "                  打印杨辉三角"
    For i = 0 To n
        For j = 0 To n + i
           If a(i, j) = 0 Then
                str = str + "    "
           End If
           If a(i, j) = 1 Then
                str = str + "1   "
           End If
           If a(i, j) > 1 And a(i, j) < 10 Then
                str = str + CStr(a(i, j)) + "   "
           End If
           If a(i, j) >= 10 And a(i, j) < 100 Then
                str = str + CStr(a(i, j)) + "  "
           End If
           If a(i, j) >= 100 Then
                str = str + CStr(a(i, j)) + " "
           End If
        Next
        Print str
        Print
        str = ""
    Next
End Sub

阅读(5067) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册