UVA 991 - Safe Salutations (卡特兰数)

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 integer n, 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 groupwith n 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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值