C - Train Problem II——卡特兰数

题目链接_HDU-1023

题目

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

思路:

仔细一看,就是让你求卡特兰数,只不过这个数的范围大了点,会爆long long ,所以要用数组来进行存储。这道题的核心应该就是让求大数对一个较小的数的乘法和除法。

而这个乘法和除法(因为是对一个较小的数的操作)所以就是简单的模拟我们平常手算对数的乘法和除法。

具体代码实现如下:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int a[2005];
void mu(int k)
{
    int c=0;
    for(int i=1;i<=2000;i++)
    {
        int u=a[i]*k+c;
        a[i]=u%10;
        c=u/10;
    }

}
void chu(int k)
{
    int c=0;
    for(int i=2000;i>=1;i--)
    {
        int u=a[i]+c;
        a[i]=u/k;
        c=u%k*10;
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
         a[1]=1;
        for(int i=n+1;i<=2*n;i++)
        {
            mu(i);
        }
        for(int i=1;i<=n+1;i++)
        {
            chu(i);
        }
        int flog=0;
        for(int i=2000;i>=1;i--)
        {
            if(a[i]!=0)
            {
                flog=1;
            }
            if(flog==1)
            {
                printf("%d",a[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/HANGANG/p/11900855.html