P5732 【深基5.习7】杨辉三角
题目描述
给出 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<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a[20][20];
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
if(j==0 || j==i) a[i][j]=1;
else a[i][j]=a[i-1][j]+a[i-1][j-1];
cout<<a[i][j]<<' ';
}
cout<<endl;
}
return 0;
}
博主的个人网站:https://codespark.cn/

浙公网安备 33010602011771号