(2)L - 卡特兰数

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.

卡特兰数,一听就不知道是什么,然后去学习了一番,就不过多描述了,具体链接http://blog.csdn.net/doc_sgl/article/details/8880468,卡特兰数有递推公式  f(n)=f(n-1)*(4*n-2)/n-1;
因为该数增加很快,n=100时,数会超大,所以要用到大数乘除。

#include<iostream>
using namespace std;
int a[110][101];//a[x][y],x表示第几个卡特兰数,a[y]则存储该数字,其中a[x][0],存储该数有几位。
void ktl();
int main()
{
 ktl();
 int n,i;
 while(cin>>n)
 {
  for(i=a[n][0];i>0;i--)//乘除时是倒序
  {
   cout<<a[n][i];
  }
  cout<<endl;
 }
}
void ktl()//先打表
{
 int i,j,k,len,temp,num;
 a[1][0]=1;
 a[1][1]=1;
 a[2][0]=1;
 a[2][1]=2;
 len=1;
 for(i=3;i<=101;i++)
 {
  temp=0;//保存进位的数
  for(j=1;j<=len;j++)
  {
   num=a[i-1][j]*(4*i-2)+temp;
   temp=num/10;
   a[i][j]=num%10;//进位处理
  }
  while(temp)//j>len时,temp进位可能不为0,故需要继续处理
  {
   a[i][++len]=temp%10;
   temp/=10;
  }
  for(j=len;j>=1;j--)//除法
  {
   num=a[i][j]+temp*10;//大数同余
   a[i][j]=num/(i+1);
   temp=num%(i+1);
  }
  while(!a[i][len])//查找数的长度
  {
   len--;
  }
  a[i][0]=len;
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值