您的位置:首页 > 大数据 > 人工智能

HDU1023 Train Problem (卡特兰数)

2017-06-08 20:33 211 查看
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.

Input

The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.

Output

For each test case, you should output how many ways that all the trains can get out of the railway.

Sample Input

1

2

3

10

Sample Output

1

2

5

16796

Hint

The result will be very large, so you may not process it by 32-bit integers.

#include<stdio.h>
#include<string.h>

int a[110][100];//大数卡特兰数
int b[110];//卡特兰数的长度

void Catalan()//求卡特兰数
{
int i,j,k,len,tmp;
a[1][0]=b[1]=1;
len=1;
for(i=2;i<=100;i++)
{
for(j=0;j<len;j++)//乘法
a[i][j]=a[i-1][j]*(4*i-2);
k=0;
for(j=0;j<len;j++)//处理相乘结果
{
tmp=k+a[i][j];
a[i][j]=tmp%10;
k=tmp/10;
}
while(k)//进位处理
{
a[i][len++]=k%10;
k/=10;
}
for(j=len-1;j>=0;j--)//除法
{
tmp=k*10+a[i][j];
a[i][j]=tmp/(i+1);
k=tmp%(i+1);
}
while(!a[i][len-1])//高位零处理
len--;
b[i]=len;
}
}

int main()
{
int n;
Catalan();
while(~scanf("%d",&n))
{
for(int i=b
-1;i>=0;i--)
printf("%d",a
[i]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: