您的位置:首页 > 其它

HDU 2032 杨辉三角

2015-07-19 16:25 204 查看
Description

还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1



[b]Input

[/b]

输入数据包含多个测试实例,每个测试实例的输入只包含一个正整数n(1<=n<=30),表示将要输出的杨辉三角的层数。



[b]Output

[/b]

对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空行。



[b]Sample
Input


[/b]

2
3



[b]Sample
Output


[/b]

1

1
1


1

1
1


1
2 1



代码:

#include<stdio.h>
#include<cstring>
#define N 100
using namespace std;
int main()
{
int t,c[N][N];
while(~scanf("%d",&t))
{
memset(c,0,sizeof(c));
for(int i=0; i<t; i++)
{
c[i][0]=1;
int k=i+1;
for(int j=1; j<=t; j++)
{
c[k][j]=c[k-1][j-1]+c[k-1][j];
}
}
for(int i=0; i<t; i++)
{
for(int j=0; j<t; j++)
{
if(c[i][j]==0)break;
else if(i == j) printf("%d\n",c[i][j]);  //这有坑》》最后一个数后面没空格
else printf("%d ",c[i][j]);
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: