注册 登录
编程论坛 VB6论坛

求助 关于杨辉三角形

自在飞叶 发布于 2006-12-17 22:03, 871 次点击
求助

关于杨辉三角形的编程

怎样通过数组来完成

1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
7 回复
#2
purana2006-12-18 09:04
Dim I as integer,j as integer
Dim Mat(10,10) as integer
For I = 1 to 10
Mat(I,I) = 1
For j = 1 to I
If j = 1 then
Mat(I,j) = 1
Else
Mat(I,j) = Mat(I-1,j-1) + Mat(I-1,j)
End if
Next
Next
For I = 1 to 10
For j = 1 to I
Print Mat(I,j) & “ “;
Next
Print
Next
#3
wyfandy2006-12-18 09:25


杨辉三角形不是那样的吧,应该是:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
#4
kiso12342006-12-18 09:48
只有本站会员才能查看附件,请 登录

如果杨辉三角是你说的那样的话,可以这样写。不过用数组感觉不是很方便,用COLLECTION会好一些。
#5
自在飞叶2006-12-18 22:13
以下是引用purana在2006-12-18 9:04:21的发言:
Dim I as integer,j as integer
Dim Mat(10,10) as integer
For I = 1 to 10
Mat(I,I) = 1
For j = 1 to I
If j = 1 then
Mat(I,j) = 1
Else
Mat(I,j) = Mat(I-1,j-1) + Mat(I-1,j)
End if
Next
Next
For I = 1 to 10
For j = 1 to I
Print Mat(I,j) & “ “;
Next
Print
Next

原来这个三角形的规律是这样的,刚开始没有找到规律所以无从下手

谢谢,已经运行正常

#6
2010-05-01 00:51
以下是引用purana在2006-12-18 09:04:21的发言:

Dim I as integer,j as integer
Dim Mat(10,10) as integer
For I = 1 to 10
    Mat(I,I) = 1
    For j = 1 to I
        If j = 1 then
            Mat(I,j) = 1
        Else
            Mat(I,j) = Mat(I-1,j-1) + Mat(I-1,j)
        End if
    Next
Next
For I = 1 to 10
    For j = 1 to I
         Print Mat(I,j) & “   “;
    Next
    Print
Next
如果是输入n行那,不确定多少行,怎么办啊??
#7
zhaoyangwx2012-04-04 12:59
考虑用动态数组
#8
未来之神2012-06-07 16:30
dim a() as integer
n=val(inputbox(""))
redim a(n+1,n+1)
for i=0 to n
    for j=0 to i
        if i=j or j=0 then
           a(i,j)=1
        else
           a(i,j)=a(i-1,j-1)+a(i-1,j)
        endif
        print tab(7*j);a(i,j);
    next
next
1