hpuoj1298: 杨辉三角2 (题目链接点击打开链接)
杨辉三角,是二项式系数在三角形中的一种几何排列。在欧洲,这个表叫做帕斯卡
三角形。帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年,比贾宪迟600年。
输入
输入一个n,表示行数。
ps:2<=n<=15.
输出
将数据按金字塔形输出,两个数据间有两个空格,数据占两个位置。
样例输入
5
样例输出
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
//下面是我的代码,因为没有处理好数字位数和空格的关系导致一直在wa
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
int m,a[16][16];
memset(a,0,sizeof(a));
cin>>m;
for(int i=1;i<=m;i++)
{
a[i][0]=1;
a[i][i-1]=1;
for(int j=1;j<i-1;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];//这是杨辉三角的规律,从第三行非首尾数字开始,每个数字等于其上方两数字之和
}
}
int t=m;
for(int i=1;i<=m;i++)
{
for (int j=1;j<=2*t;j++)
{
cout<<" ";//这是为了打印出每一行开始的空格,空格数是以2*m+1开始,之后每行减2
}
t-=1;
for(int k=0;k<i;k++)
{
cout<<setw(2)<<a[i][k];//注意位数与空格
cout<<" ";
}
cout<<endl;
}
return 0;
}