杨辉三角

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int x, y, a[10][10] = { 0 };
 5     for (x = 0; x < 10;x++)        //把首列和对角线赋值为1,以便后面计算
 6     {
 7         a[x][x] = 1;
 8         a[x][0] = 1;
 9     }
10     for (x = 2; x < 10;x++)                        //循环  
11     {
12         for (y = 1; y <=x-1;y++)        //不能漏掉条件语句的= 因为这是算到数组行末尾前一位
13         {
14             a[x][y] = a[x - 1][y] + a[x - 1][y - 1];        //求出结果
15         }
16     }
17     for (x = 0; x < 10;x++)            
18     {
19         for (y = 0; y <=x;y++)
20         {
21             printf("%4d ", a[x][y]);
22         }
23         printf("\n");
24     }
25     return 0;
26 }

 

posted @ 2020-03-16 21:41  新生代农民工  阅读(94)  评论(0)    收藏  举报