【杭电oj2032】杨辉三角

杨辉三角

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 56722    Accepted Submission(s): 23663

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  

Author lcy  

Source
C语言程序设计练习(五)  

Recommend lcy   |   We have carefully selected several similar problems for you:  
1000 
2048 
2046 
2044 
1108   

根据杨辉三角公式递推。

#include<stdio.h>
int a[33][33];
int main() {
    int n;
    while(scanf("%d",&n)!=EOF) {
        int i,j,k;
        for(i=0; i<n; i++)
            a[i][0]=1;
        for(i=1; i<n; i++)
            for(j=1; j<n; 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;
}

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/better_space/article/details/52207209
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞