P5732 杨辉三角题解

题目传送门

总结

1、递推的初始条件
if (j == 0 || j == i) a[i][j] = 1;

2、递推式
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];

#include <bits/stdc++.h>

using namespace std;
const int N = 25;
int a[N][N];
int n;

int main() {
    cin >> n;
    //遍历每一行
    for (int i = 0; i <= n - 1; i++) {
        //遍历每一列
        for (int j = 0; j <= i; j++) {
            //第一列,最后一列都是1
            if (j == 0 || j == i) a[i][j] = 1;
                //递推式
            else a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
posted @ 2021-07-21 15:06  糖豆爸爸  阅读(114)  评论(0编辑  收藏  举报
Live2D