P5732 【深基5.习7】杨辉三角

116 阅读1分钟

题目描述

给出 n(n≤20),输出杨辉三角的前 n 行。

如果你不知道什么是杨辉三角,可以观察样例找找规律。

输入格式

输出格式

输入输出样例

输入 #1

6

输出 #1

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

题解:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int i,j,a[n+1][n+1];
  for(i=1;i<=n;i++)
{		
  a[i][i]=1;
  a[i][1]=1;
}
 for(i=3;i<=n;i++)			 
 for(j=2;j<=i-1;j++) 
 a[i][j]=a[i-1][j-1]+a[i-1][j]; 
 for (i=1;i<=n;i++)
{			
  for (j=1;j<=i;j++)			
  cout<<a[i][j]<<" ";	
  cout<<endl;
}
  cout<<endl;
  return 0;
}

杨辉三角研究:

C++编程 杨辉三角_TomLazy的博客-CSDN博客_杨辉三角