题目描述
给出 �(�≤20)n(n≤20),输出杨辉三角的前 �n 行。
如果你不知道什么是杨辉三角,可以观察样例找找规律。
输入格式
无
输出格式
无
输入输出样例
输入:
6
输出:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
原代码:
#include<iostream>
using namespace std;
int a[22][22]={};
int main(){
int n;
cin>>n;
a[1][1]=1;
for(int i=2;i<=n;i++){
for(int j=1;j<=n;j++){
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}
cout<<"1"<<endl;
for(int i=2;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i][j]>0){
if(j==1){
cout<<"1";
}
else{
cout<<" "<<a[i][j];
}
}
}
cout<<endl;
}
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
*/
}