Game ofConnections

Poj 2084

 

Game ofConnections

TimeLimit1000ms

Description

This is asmall 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 numberpairs. 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 2nnumbers, can you tell me in how many different ways can you connect the numbersinto pairs? Life is harder, right?

Input

Each lineof 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 eachn, print in a single line the number of ways to connect the 2n numbers intopairs.

Sample Input

2
3
-1

Sample Output

2
5

 

卡塔兰数




问题分析:好吧,纯粹的卡特兰数,加大数乘法,并不是很懂……


两种,别人写的和自己的
#include<stdio.h>
#include<string.h>

int ans[102][100];

void list()
{
	ans[0][0] = ans[1][0] = 1;
	int i, j;
	for(i = 2; i < 102; i ++)
	{
		int c = 0;
		for(j = 0; j < 100; j ++)
		{
			ans[i][j] = ans[i-1][j]*(4*i-2)+c;
			c = ans[i][j]/10;
			ans[i][j] %= 10;
		}
		int z = 0;
		for(j = 99; j >= 0; j --)
		{
			z= z*10+ans[i][j];
			ans[i][j] = z/(i+1);
			z %= (i+1);
		}
	}
}
int main(){
	int t;
	
	list();
	while(~scanf("%d", &t))
	{
		int i = 99;
		
		if (t == -1)
			break;
		while(ans[t][i] == 0) 
			i --;
		for(;i >= 0; i--) 
			printf("%d", ans[t][i]); 
		
		printf("\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
int a[104][200];
void init();
int strlen_new(int r[]);
int main(){
    int n;
    init();
    scanf("%d",&n);
    while(n!=-1){
        for(int i = strlen_new(a[n]);i>=0;i--){
            printf("%d",a[n][i]);
        }
        printf("\n");
        scanf("%d",&n);
    }
    return 0;
}

int strlen_new(int r[]){
    for(int i=150;i>=0;i--){
        if(r[i]){
            return i;
        }
    }
}

void strcpy_new(int a[],int b[]){
    for(int i=0;i<=150;i++){
        a[i] = b[i];
    }
}


void init(){
    memset(a,0,sizeof(a));
    a[0][0] = 1;
    a[1][0] = 1;
    for(int i=2;i<=101;i++){
        int temp = (2*(2*(i-1)+1));
        int e = strlen_new(a[i-1]);
        for(int j=0;j<=e;j++){    //mul
            a[i][j] += a[i-1][j]*temp;
            int t = a[i][j];
            int k=j+1;
            t/=10;
            while(t){
                a[i][k] += t%10;
                k++;
                t/=10;
            }
            a[i][j]= a[i][j]%10;
        }
        int c[200];            //chu
        memset(c,0,sizeof(c));
        int t = i+1;
        int chu = 0;
        int ee = strlen_new(a[i]);
        for(int k=ee;k>=0;k--){
            chu= a[i][k]+chu*10;
            if(chu/t>0){
                c[k] = chu/t;
                chu%=t;
            }

        }
        strcpy_new(a[i],c);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值