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;
}
相關文章
相關標籤/搜索