HDU2032 杨辉三角

找出规律:f[i][j] = f[i-1][j]+f[i-1][j-1],注意边界问题,防止越界访问

#include <stdio.h>
#include <string.h>
#define ONLINE_JUDGE

int
main(int argc, char **argv) {
    int n, i, j;
    int triangle[31][31];
    //int is_first = 1;
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    memset(triangle, 0, sizeof(triangle));
    triangle[1][1] = 1;
    for (i = 2; i <= 30; ++ i)
        for (j = 1; j <= i; ++ j)
            triangle[i][j] = triangle[i - 1][j] + triangle[i - 1][j - 1];
    while (scanf("%d", &n) == 1) {
        //if (!is_first) printf("\n");
        for (i = 1; i <= n; ++ i) {
            for (j = 1; j <= i - 1; ++ j) {
                printf("%d ", triangle[i][j]);
            }
            printf("%d\n", triangle[i][i]);
        }
        printf("\n");
        //is_first = 0;
    }
    #ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    #endif // ONLINE_JUDGE
    return 0;
}

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