hdu/hdoj 1134 Game of Connections
2013-08-29 21:09
447 查看
卡特兰数,就模板了
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define base 10000//数组一个单位存的是10000进制
#define maxx 100//数组长度
void multiply(int a[],int max,int b)
{
int i,array=0;
for(i=max-1;i>=0;i--)
{
array+=b*a[i];
a[i]=array%base;
array/=base;
}
//for(i=0;i<max;i++) printf("%d",a[i]);
// printf("");
}
void divide(int a[],int max,int b)
{
int i,div=0;
for(i=0;i<max;i++)
{
div=div*base+a[i];//如果前面div不为0则加上div*base 如果为0 不借位就算了 假如a[i-1]=0000,a[i]=0006 b=3,这时前面div为0 不借位就算了得到2
a[i]=div/b;
div%=b;
}
//for(i=0;i<max;i++) printf("%d",a[i]);
//printf("");
}
int main()
{
int n,i;
int a[101][maxx]; //只是打印了从1到100的卡特兰数 数组就开了100
memset(a[1],0,maxx*sizeof(int));//初始化a[1]为0
for(i=2,a[1][maxx-1]=1;i<101;i++)
{
memcpy(a[i],a[i-1],maxx*sizeof(int));//把a[i-1]复制进a[i]
multiply(a[i],maxx,4*i-2);
divide(a[i],maxx,i+1);
}
while(scanf("%d",&n) && n!=-1)//输入n对应输出卡特兰数
{
for(i=0;i<maxx&&a
[i]==0;i++);
printf("%d",a
[i++]);
for(;i<maxx;i++)
{
printf("%04d",a
[i]);
}
printf("");
printf("\n");
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define base 10000//数组一个单位存的是10000进制
#define maxx 100//数组长度
void multiply(int a[],int max,int b)
{
int i,array=0;
for(i=max-1;i>=0;i--)
{
array+=b*a[i];
a[i]=array%base;
array/=base;
}
//for(i=0;i<max;i++) printf("%d",a[i]);
// printf("");
}
void divide(int a[],int max,int b)
{
int i,div=0;
for(i=0;i<max;i++)
{
div=div*base+a[i];//如果前面div不为0则加上div*base 如果为0 不借位就算了 假如a[i-1]=0000,a[i]=0006 b=3,这时前面div为0 不借位就算了得到2
a[i]=div/b;
div%=b;
}
//for(i=0;i<max;i++) printf("%d",a[i]);
//printf("");
}
int main()
{
int n,i;
int a[101][maxx]; //只是打印了从1到100的卡特兰数 数组就开了100
memset(a[1],0,maxx*sizeof(int));//初始化a[1]为0
for(i=2,a[1][maxx-1]=1;i<101;i++)
{
memcpy(a[i],a[i-1],maxx*sizeof(int));//把a[i-1]复制进a[i]
multiply(a[i],maxx,4*i-2);
divide(a[i],maxx,i+1);
}
while(scanf("%d",&n) && n!=-1)//输入n对应输出卡特兰数
{
for(i=0;i<maxx&&a
[i]==0;i++);
printf("%d",a
[i++]);
for(;i<maxx;i++)
{
printf("%04d",a
[i]);
}
printf("");
printf("\n");
}
return 0;
}
相关文章推荐
- hdoj-1134-Game of Connections【卡特兰数】
- hdoj 1134 Game of Connections【水】【java】
- HDOJ 1134 Game of Connections 简单解题报告
- 【HDOJ】1134 Game of Connections
- HDOJ HDU 1134 Game of Connections
- hdu 1134 Game of Connections(卡特兰数模板题)
- HDU 1134 Game of Connections
- 1134 ---( Game of Connections )
- HDU 1134 Game of Connections (高精度卡特兰数)
- 高精度-HDU-1134-Game of Connections
- HDU1134 Game of Connections(大数乘法+打表)
- hdu 1134 Game of Connections(卡特兰(Catalan)公式)
- hdu 1134 Game of Connections
- [Catalan] HDU 1134 Game of Connections
- HDU ACM 1134 Game of Connections / 1130 How Many Trees?(卡特兰数)
- hdu 1134 game of connections(Catalan Number)
- hdu1134 Game of Connections 卡特兰数(大数)
- HDU 1134 Game of Connections
- [ACM] hdu 1134 Game of Connections(大数+Catalan数)
- hdu 题目1134 Game of Connections (卡塔兰数 catalan number)