C++ 打印杨辉三角

 1 #include <iostream>
 2 #include  <iomanip>
 3 using namespace std;
 4 #define N 100
 5 
 6 int main()
 7 {
 8     int a[N][N] = {0};
 9     int i, j, n = 6;
10     cin>>n;
11      
12     for(i=1;i<=n;i++){
13         a[i][1] = a[i][i] = 1;  /*1.第一列和对角线的数都是1*/
14     }
15         
16     for(i=3;i<=n;i++)
17     {
18         for(j=2;j<=i-1;j++){
19             a[i][j]=a[i-1][j-1]+a[i-1][j];  /*2.除两边的数, 等于左上/上两数之和*/ 
20         }          
21     }
22         
23     for(i=1;i<=n;i++)
24     {
25         cout<<setw((n-i)*3)<<' ';
26         
27         for(j=1;j<=i;j++)  
28         {
29             cout<<setw(6)<<a[i][j];  
30         }
31               
32         cout<<endl;
33     }
34     
35     return 0;
36 }

猜你喜欢

转载自www.cnblogs.com/GoldenEllipsis/p/11614503.html
今日推荐