杨辉三角
#include <iostream>
using namespace std;
int main(){
int numRows;
cin>>numRows;
int a[numRows][numRows]={0};
cout<<"["<<endl;
for(int i=0;i<numRows;i++){
cout<<"[";
for(int j=0;j<=i;j++){
if(j==0){
a[i][j]=1;
if(i!=0) cout<<1<<",";
else cout<<1;
}
else if(j==i){
a[i][j]=1;
cout<<1;
}
else{
a[i][j]=a[i-1][j-1]+a[i-1][j];
cout<<a[i][j]<<",";
}
}
cout<<"]";
if(i!=numRows-1)
cout<<","<<endl;
else cout<<endl;
}
cout<<"]"<<endl;
return 0;
}
输出结果:
搞不太懂乐扣的编写方式,直接DEV写了。