欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UVA991 Safe Salutations【卡特兰数+打表】

程序员文章站 2022-04-02 19:01:15
...

As any minimally superstitious person knows all too well, terrible things will happen when four persons do a crossed handshake.
    You, an intrepid computer scientist, are given the task of easing the burden of these people by providing them with the feasible set of handshakes that include everyone in the group while avoiding any such crossings. The following figure illustrates the case for 3 pairs of persons:
UVA991 Safe Salutations【卡特兰数+打表】
Input
The input to this problem contains several datasets separated by a blank line. Each dataset is simply an integer n, the number of pairs of people in the group, with 1 ≤ n ≤ 10.
Output
The output is equally simple. For each dataset, print a single integer indicating the number of possible crossless handshakes that involve everyone in a group with n pairs of people. Print a blank line between datasets.
Sample Input
4
Sample Output
14

问题链接UVA991 Safe Salutations
问题简述:(略)
问题分析
    。
    。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA991 Safe Salutations */

#include <bits/stdc++.h>

using namespace std;

const int N = 10;
long long c[N + 1];

// 初始化卡特兰数数组
void init()
{
    c[0] = 1LL;
    for(int i = 1; i <= N; i++)
        c[i] = c[i - 1] * (2 * i + 1) * 2 / (i + 2);
}

int main()
{
    init();

    int n, flag = 0;
    while(~scanf("%d", &n)) {
        if(flag) printf("\n");
        else flag = 1;
        printf("%lld\n", c[n - 1]);
    }

    return 0;
}
相关标签: ACM数学 算法