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 main() {
    int n;
    cin >> n;
    int i, j;
    for (i = 0; i <= n - 1; i++) {
        for (j = 0; j <= i; j++) {
            if (j == 0 || j == i) a[i][j] = 1;
            else a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15039584.html