C - Train Problem II——卡特兰数

题目连接_HDU-1023php

题目数组

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.spa

inputblog

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.get

outputinput

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

Sample Inputit

1io

2class

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;
}
相关文章
相关标签/搜索