UVA 991 - Safe Salutations (卡特兰数)

来源:互联网 发布:软交换网络协议 编辑:程序博客网 时间:2024/05/21 11:05

Safe Salutations

Problem

As any minimally superstitious person knows all too well, terriblethings will happen when four persons do a crossed handshake.

You, an intrepid computer scientist, are given the task of easing theburden of these people by providing them with the feasible set ofhandshakes that include everyone in the group while avoiding any suchcrossings. The following figure illustrates the case for 3 pairs ofpersons:

Input

The input to this problem contains several datasets separated by a blank line. Each dataset is simply an integern, the number ofpairs of people in the group, with 1 ≤ n ≤ 10.

Output

The output is equally simple. For each dataset, print a single integer indicating the numberof possible crossless handshakes that involve everyone in a groupwithn pairs of people. Print a blank line between datasets.

Sample Input

4

Sample Output

14
题意:给定n,表示在一个圆上选n对点。求不所有直线不相交的方案有几种。

思路:直线不能穿插。所以对于n对来说,可以选一对直线,那么剩下的必须是偶数,可以分割为0, n -1, 1, n -2 ... n - 1, 0对。所以对于当前的f(n) = f(0)f(n - 1) + f(1)f(n - 2) + f(2)f(n -3) +...+ f(n -1)(0)。显然是一个卡特兰数。由于n只有10.直接递推打表出所有答案。

代码:

#include <stdio.h>#include <string.h>int n, i, j, dp[10];int main() {dp[0] = dp[1] = 1; dp[2] = 2;for (i = 3; i <= 10; i ++)for (j = 0; j < i; j ++) {dp[i] += dp[j] * dp[i - j - 1];}int t = 0;while (~scanf("%d", &n)) {if (t) printf("\n");t ++;printf("%d\n", dp[n]);}return 0;}


原创粉丝点击