# poj2084 Game of Connections

241人阅读 评论(0)

Description
This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . . . , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another.
And, no two segments are allowed to intersect.

It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?

Input
Each line of the input file will be a single positive number n, except the last line, which is a number -1.

You may assume that 1 <= n <= 100.

Output

For each n, print in a single line the number of ways to connect the 2n numbers into pairs.

Sample Input
2
3

-1

Sample Output
2

5

1.括号化问题：矩阵链乘： P=a1×a2×a3×……×an，依据乘法结合律，不改变其顺序，只用括号表示成对的乘积，试问有几种括号化的方案？(h(n))

2.出栈次序问题：一个栈(无穷大)的进栈序列为123，…，n，有多少个不同的出栈序列?

3.凸多边形的三角剖分问题：求将一个凸多边形区域分成三角形区域的方法数。

4.用给定节点组成二叉树的问题：给定N个节点，能构成多少种不同的二叉树？（能构成hN）个）

#include<iostream>
using namespace std;
int cal[109][109]; ///卡特兰数cal[i][j];i代表第i个卡特兰数，j代表当前卡特兰数的第j位
int len[109]; ///用于存储各个卡特兰数的长度

void getCal()
{
len[1] = 1;
cal[1][0] = 1;

int carry;  ///用于控制进位
int temp;
int tempLen = 1;
for(int i=2;i<=100;i++)
{
for(int j=0;j<tempLen;j++)  ///先进行相乘
{
cal[i][j] = (4*i-2)*cal[i-1][j];
}
carry = 0;
for(int j=0;j<tempLen;j++)  ///把各个数分配的不同的位上
{                           ///与手算乘法的道理一样：从低位往高位算
temp = carry + cal[i][j];
cal[i][j] = temp%10;
carry = temp/10;
}

while(carry)
{
cal[i][tempLen++] = carry%10;
carry /= 10;
}

carry = 0;

for(int j=tempLen-1;j>=0;j--)  ///处理除法，与手算除法的道理一样：从高位往低位算
{
temp = carry*10 + cal[i][j];
cal[i][j] = temp/(i+1);
carry = temp%(i+1);
}

while(cal[i][tempLen-1]==0)  ///如果最高位是0的话，则不算数
{
tempLen --;
}

len[i] = tempLen;
}
}
int main()
{
getCal();
int n;
while(cin>>n)
{
if(-1==n)
{
break;
}
for(int i=len[n]-1;i>=0;i--)
{
cout<<cal[n][i];
}
cout<<endl;
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：8496次
• 积分：342
• 等级：
• 排名：千里之外
• 原创：27篇
• 转载：2篇
• 译文：0篇
• 评论：2条
文章存档
阅读排行
评论排行