HDU 2032

来源:互联网 发布:声鉴软件 编辑:程序博客网 时间:2024/05/01 10:51
Problem Description
还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
 

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

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

Sample Input
2 3
 

Sample Output
11 111 11 2 1
#include<iostream>using namespace std;int main(){//    freopen("E:\\in.txt","r",stdin);    int n;    while(cin>>n){        int b[50],i,j,*s,*t=b;        memset(b,0,sizeof(b));        b[0]=b[1]=1;        cout<<"1"<<endl;    if(n==1) {cout<<endl;continue;}                    cout<<"1 1"<<endl;  if(n==2) {cout<<endl;continue;}        for(i=3;i<=n;i++){            int cnt=1,ins=1,*p,a[50];            bool flag=true;            s=t;                  for(j=0;s[j];j++)                a[j]=*(s+j);                    for(j=0;j<i-2;j++){                t[ins++]=a[cnt-1]+a[cnt];                        cnt++;            }            t[ins]=1;            for(j=0;t[j];j++){                            if(flag) flag=false;                else putchar(' ');                cout<<t[j];            }            cout<<endl;        }cout<<endl;    }    return 0;}


0 0