卡特兰数

Game of Connections

 

TimeLimit 1000ms

Description

This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . . . , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. 
And, no two segments are allowed to intersect. 
It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?

Input

Each line of the input file will be a single positive number n, except the last line, which is a number -1. 
You may assume that 1 <= n <= 100.

Output

For each n, print in a single line the number of ways to connect the 2n numbers into pairs.

Sample Input

2

3

-1

Sample Output

2

5

 

//卡塔兰数  h[n]=h[n-1]*(4*n-2)/(n+1)。h[0]=h[1]=1

题意:输入一个整数n,用2n个数围成一个圆圈。然后用n条直线连成n对数 (连线不能相交),求有多少种连法?

其实就是大数乘法除法

#include<stdio.h>
#include<string.h>
const int M=200;
const int N=10000;//每四位存入数组中的一个位置
int n;
void mul(int m,int *w)//大数乘法   二维数组对应指针
{
int i,r=0,c=0;
for(i=M;i>=0;i--)  //下标起始点
{
r=c+w[i]*m;//注意逻辑
w[i]=r%N;
c=r/N;
}
return; 
}
void div(int m,int *w)//大数除法
{
int i,r=0,c=0;
for(i=0;i<=M;i++)
{
r=c*N+w[i];
w[i]=r/m;
c=r%m;
}
return;
}
int main()//因为要输入多次,n最大为100,所以将所有情况都求出,到时候直接输出就好
{
int i,j;
int a[111][M+1];    
  //注意下标范围啊!!!!!一点不注意就不对
memset(a,0,sizeof(a));
a[0][M]=1;//初始化 h[0]=h[1]=1,即n=1或0时,最后一位即最低位是1,前面全为0,一开始想错了,以为1在最高位
a[1][M]=1;
for(i=2;i<=100;i++)
{
  memcpy(a[i],a[i-1],sizeof(a[i])); //结论式 h[n]=h[n-1]*(4*n-2)/(n+1),这条语句使得直接在h[n]上进行乘除操作即可

  mul((4*i-2),a[i]);
  div((i+1),a[i]);
}
while(~scanf("%d",&n))
    {
    if(n==-1)
    break;
i=0;
while(a[n][i]==0)//可能数位达不到M,需要舍弃前面多余的0
i++;
printf("%d",a[n][i]);
for(j=i+1;j<=M;j++)//数都是由我们计算得来的,其后不会有多余的0
printf("%04d",a[n][j]);
printf("\n");
    }
return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值