<FONT face=Tahoma>(一)杨辉三角形
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
这是我国宋朝数学家杨辉在公元1261年著书《详解九章算法》里画的一张图,这便是著名的杨辉三角形。
(二)程序设计
由笔算过程已经分析出算法,可利用循环控制变量算出。我们除掉最上层仅有一个数字的1,其下算做第一行,依次可递推至任意N行。可随机输入要算的N行数字置入数组M(15)中,而后赋给数组IM(90)。输出数组IM(90)时,形象地打印出三角形的层次和各行各位的数字,并打印汉语拼音“杨辉三角形”。本程序设计最多为12行。
FORTRAN语言源程序:
INTEGER M(15),IM(90)
WRITE (*,10)
10 FORMAT(10X,22HYang-Hui San Jiao Xing/5X,’N
READ(*,20)N
20 FORMAT(I2)
JJ=0
DO 44 I=1,N
DO 30 L=1,N
M(L)=0
30 CONTINUE
K=1
M(1)=1
L=I+1
DO 50 J=1,L
K=K*(I-J+1)/J
M(J+1)=K
50 CONTINUE
DO 40 IJ=1,L
JI=JJ+IJ
IM(JI)=M(IJ)
40 CONTINUE
JJ=JJ+L
44 CONTINUE
WRITE(5,60) (IM(I),I=1,JJ)
60 FORMAT(33X,2I6/,30X,3I6/,28X,4I6/,25X,5I6/,22X,6I6/,
1 19X,7I6/,16X,8I6/,13X,9I6/,10X,10I6/,7X,11I6/,4X,12I6/,
2 1X,13I6/)
END
BASIC语言源程序:
10 DIM m(15): DIM im(90)
20 PRINT "Yang-Hui San Jiao Xing"
30 INPUT n
40 jj = 0
50 FOR i = 1 TO n
60 FOR l = 1 TO n
70 m(l) = 0
80 NEXT l
90 k = 1: m(1) = 1
100 l = i + 1
110 FOR j = 1 TO l
120 k = k * (i - j + 1) / j
130 m(j + 1) = k
140 NEXT j
150 FOR ij = 1 TO l
160 ji = jj + ij
170 im(ji) = m(ij)
180 NEXT ij
190 jj = jj + l
200 NEXT i
210 j = 1
220 FOR i = 1 TO n
230 FOR k = 1 TO i + 1
240 PRINT TAB(30 - i * 3 + k * 6); im(j);
250 j = j + 1
260 NEXT k
270 PRINT
280 NEXT i</FONT>
<>
<><FONT style="FONT-SIZE: 12px; FONT-FAMILY: MS Shell Dlg, Tahoma, sans-serif, 宋体">__________________
</FONT></P> |