题目描述
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
Note that the row index starts from 0.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
输入
一个正整数n(n<=100)
输出
在一行内输出n+1个数
样例输入
3
样例输出
1 3 3 1
分析:看到此类数据很自然的会想到二维数组,利用二维数组的层次性,由下至上计算相应位置的数值。
题解:
#include <iostream>
using namespace std;
int main()
{
int n,m,a[100][100]={0};
cin>>m;
a[1][1]=a[2][1]=a[2][2]=1;
for(int i=0;i<100;i++)
a[i][1]=1;
n=m+1; //此处由于没看清题意,将意思误解为n行n数,实际为n行n+1数,故利用m代替
for(int t=3;t<n+1;t++)
{
for(int j=t;j<t+1;j++)
{
for(int i=2;i<t+1;i++)
{
a[j][i]=a[j-1][i-1]+a[j-1][i];
}
}
}
for(int i=1;i<n+1;i++)
{
cout<<a[n][i]<<" ";
}
return 0;
}