HDU:2032 楊輝三角

主要就是每一行最後一個數字後面不能有空格,不然會presentation error。


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
1
1 1

1
1 1

1 2 1

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main()
{
    int n,i,j,s,sum;
    int a[50][50]={{1},{1,1}};
    while(scanf("%d",&n)!=EOF){
        for(i=2;i<n;i++){
            a[i][0]=1;
            a[i][i]=1;
            for(j=1;j<i;j++){
                a[i][j]=a[i-1][j-1]+a[i-1][j];
            }
        }
        for(i=0;i<n;i++){
            for(j=0;j<=i;j++){
                if(j==i)printf("%d",a[i][j]);
                else printf("%d ",a[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章