其他分享
首页 > 其他分享> > 洛谷P1044 [NOIP2003 普及组] 栈(DP、卡特兰数、递推)

洛谷P1044 [NOIP2003 普及组] 栈(DP、卡特兰数、递推)

作者:互联网

DP:

#include <bits/stdc++.h>
using namespace std;

int p[20][20];
int main() {
    int n;
    cin >> n;
    for (int i = 0; i <= n; ++i) {
        p[i][0] = 1;
    }
    for (int j = 1; j <= n; ++j) {
        for (int i = 0; i <= n; ++i) {
            if (i >= 1) {
                p[i][j] = p[i - 1][j] + p[i + 1][j - 1];
            }
            if (i == 0) {
                p[i][j] = p[i + 1][j - 1];
            }
        }
    }
    cout << p[0][n] << endl;
    return 0;
}

卡特兰数:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ULL unsigned long long

int n;
int p[20];
int main() {
    cin >> n;
    p[0] = p[1] = 1;
    for (int i = 2; i <= n; ++i) {
        for (int j = 0; j < n; ++j) {
            p[i] += p[j] * p[i - j - 1];
        }
    }
    cout << p[n] << endl;
    return 0;
}

标签:std,main,20,NOIP2003,int,洛谷,include,卡特兰
来源: https://blog.csdn.net/b_b_lai_lai/article/details/114241792