杨辉三角 HDU2032

来源:互联网 发布:windows账户登录不了 编辑:程序博客网 时间:2024/05/19 19:58
F - 杨辉三角
Crawling in process...Crawling failedTime Limit:1000MS    Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
SubmitStatus Practice HDU 2032 uDebug

Description

Input

Output

Sample Input

Sample Output

Hint

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

1

1 1


1

1 1

1 2 1


//杨辉三角

 1
                         1   1   
                       1   2   1   
                     1   3   3   1   
                   1   4   6   4   1   
                 1   5   10  10  5   1   
               1   6   15  20  15  6   1   
             1   7   21  35  35  21  7   1   
           1   8   28  56  70  56  28  8   1   
         1   9   36  84  126 126 84  36  9   1   
       1   10  45  120 210 252 210 120 45  10  1   
     1   11  55  165 330 462 462 330 165 55  11  1    
   1   12  66  220 495 792 924 792 495 220 66  12  1
...
 
#include <stdio.h>#include <stdlib.h>int main(){    int i,j;    int n;    int former[31];//上一行的数据    former[1]=former[2]=1;    int temp[31];//存储当前数据    temp[1]=1;    while(scanf("%d",&n)!=EOF){        for(i=1;i<=n;i++){            printf("%d",1);            if(i>=2)                putchar(' ');            for(j=2;j<=i-1;j++){                printf("%d ",former[j-1]+former[j]);                temp[j]=former[j-1]+former[j];            }            temp[i]=1;            memcpy(former,temp,sizeof(int)*(i+1));            if(i>=2)                printf("%d",1);            putchar('\n');        }        putchar('\n');    }    return 0;}


1 0
原创粉丝点击