例5:杨辉三角

输入:n 输出:前n行的数列 样例输入:5 样例输出:(下图)

 

 

 

 

#include<bits/stdc++.h>
using namespace std;
int main(){
     int f[31][31]={};
     int n;
     cin>>n;
     f[1][1]=1;
     for(int i=2;i<=n;i++){
         for(int j=1;j<=i;j++){
             f[i][j]=f[i-1][j-1]+f[i-1][j];
         }
     }
     for(int i=1;i<=n;i++){
         for(int j=1;j<=i;j++){
             cout<<f[i][j]<<" ";
         }
         cout<<endl;
     }
     return 0;
}

 

posted @ 2022-07-01 20:46  xxsy387  阅读(83)  评论(0)    收藏  举报