楊輝3角形
發(fā)表時(shí)間:2023-08-22 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要](一)楊輝三角形 1 1 1 1 2 ...
(一)楊輝三角形
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
這是我國(guó)宋朝數(shù)學(xué)家楊輝在公元1261年著書《詳解九章算法》里畫的一張圖,這便是著名的楊輝三角形。
(二)程序設(shè)計(jì)
由筆算過(guò)程已經(jīng)分析出算法,可利用循環(huán)控制變量算出。我們除掉最上層僅有一個(gè)數(shù)字的1,其下算做第一行,依次可遞推至任意N行?呻S機(jī)輸入要算的N行數(shù)字置入數(shù)組M(15)中,而后賦給數(shù)組IM(90)。輸出數(shù)組IM(90)時(shí),形象地打印出三角形的層次和各行各位的數(shù)字,并打印漢語(yǔ)拼音“楊輝三角形”。本程序設(shè)計(jì)最多為12行。
FORTRAN語(yǔ)言源程序:
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語(yǔ)言源程序:
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