HDU1134——Game of Connections (大数乘法,除法)+ (卡特兰数列)

本文深入探讨了一款古老游戏的数学奥秘,通过解析卡特兰数列,解决游戏中如何以不同方式连接2n个数字的问题。文章详细介绍了游戏规则、输入输出格式、解题思路及代码实现,揭示了复杂问题背后的简洁数学解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Game of Connections

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 14   Accepted Submission(s) : 10
Problem 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
 


 

Source
Asia 2004, Shanghai (Mainland China), Preliminary
 


   对于这道题,半天推导不出来,什么东西。唯有百度解决问题;发现了卡特兰数列,

      竟然是大数模拟乘法,及除法。看了看书,又将大数乘法,除法搞定。。。  

 

#include<stdio.h>
#include<string.h>
int map[110][110];
int main()
{
	int i,j;
	int len=1;
	int temp=0;
	int r=0;
	int n; 
	map[1][0]=1;
	//从第二项开始模拟乘法
	for(i=2;i<=100;i++)
	{
		for(j=0;j<len;j++)
		{
			map[i][j]=map[i-1][j]*(4*i-2);//从低位依次相乘 
		}
		//对结果进行处理
		r=0;
		for(j=0;j<len;j++)
		{
			temp=map[i][j]+r;//是否超过10; 
			map[i][j]=temp%10;
			r=temp/10;
		} 
		//遇见高位超出限制的话
		for(;r;)
		{
			map[i][len]=r%10;
			r/=10;
			len++;
		} 
		//同时进行模拟相除
		//len--;
		r=0;
		for(j=len-1;j>=0;j--)
		{
			temp=map[i][j]+r*10;//去高位进行相除 
			map[i][j]=temp/(i+1);
			r=temp%(i+1); //有保留的话,下次调用 
		} 
		//别忘记去掉高位的0
		while(!map[i][len-1])  len--; 
	} 
	while(scanf("%d",&n)&&n!=-1)
	{
		int t=100;
		while(!map[n][t])  t--;
		for(;t>=0;t--)
		{
			printf("%d",map[n][t]);
		}
		printf("\n");
	}
return 0;
	
	
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值