基础练习 杨辉三角形
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a[110],b[110];
if(n==1)
{
cout<<1;
}
else if(n==2)
{
cout<<1<<endl;
cout<<1<<" "<<1;
}
else
{
cout<<1<<endl;
cout<<1<<" "<<1<<endl;
a[0]=1,a[1]=1;
for(int i=3;i<=n;i++)
{
b[0]=1;b[i-1]=1;
for(int j=0;j<i-2;j++)
{
b[j+1]=a[j]+a[j+1];
}
for(int j=0;j<i;j++)
{
if(j==0)
{
cout<<b[j];
a[j]=b[j];
}
else
{
cout<<" "<<b[j];
a[j]=b[j];
}
}
cout<<endl;
}
}
return 0;
}