HDU 1131 Count the Trees 大数计算
题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式。这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方案。因为数字比较大,所以要用高精度的方法也就是用字符数组来做,我分别写了三个函数,一个算加法,一个算乘法,最后一个打表,等打出表来最后只要判断一下输入的数是第几个,直接输出就行了,下面是我的代码,第一次写高精度的这种大数处理,可能看上去比较繁琐= =
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char ans[][];
char ads[],mus[];
char jc[][];
char res[][]; int multiply(char* a1,char* b1)
{
memset(mus,,sizeof(mus));
int i,j,k;
int len,len1,len2;
len1=strlen(a1);
len2=strlen(b1);
int mut=,t;
bool va[];
int s[],adt[];
char a[],b[];
memset(s,,sizeof(s));
memset(adt,,sizeof(adt));
memset(va,,sizeof(va));
for(i=len1-,j=;i>=;i--)
{
a[i]=a1[j];
j++;
}
for(i=len2-,j=;i>=;i--)
{
b[i]=b1[j];
j++;
}
for(i=;i<len1;i++)
{
mut=;
for(j=;j<len2;j++)
{
t=(a[i]-'')*(b[j]-'')+mut;
mut=;
if(t>=)
{
mut=t/;
t=t%;
}
s[i+j]=t+s[i+j]+adt[i+j];
va[i+j]=;
adt[i+j]=;
if(s[i+j]>=)
{
if(!va[i+j+])
adt[i+j+]=s[i+j]/+adt[i+j+];
else
adt[i+j+]=s[i+j]/;
s[i+j]=s[i+j]%;
}
}
s[i+j]=mut;
}
s[i+j-]=mut+adt[i+j-];
if(s[i+j-]!=)
k=i+j-;
else
k=i+j-;
for(i=k,j=;i>=;i--)
{
mus[i]=(s[j]+'');
j++;
}
return ;
} int additive(char* a,char* b)
{
memset(ads,,sizeof(ads));
int len,len1,len2;
int i;
int ad[];
len1=strlen(a);
len2=strlen(b);
if(len1==len2)
{
len=len1;
}
else if(len1>len2)
{
len=len1;
for(i=len;i>=len-len2;i--)
{
b[i]=b[i-len+len2];
}
for(i=len-len2-;i>=;i--)
{
b[i]='';
}
}
else if(len1<len2)
{
len=len2;
for(i=len;i>=len-len1;i--)
{
a[i]=a[i-len+len1];
}
for(i=len-len1-;i>=;i--)
{
a[i]='';
}
}
int t=;
for(i=len-;i>=;i--)
{
ad[i]=(a[i]-'')+(b[i]-'')+t;
t=;
if(ad[i]>=)
{
t++;
ad[i]=ad[i]-;
ads[i]=ad[i]+'';
}
else
{
ads[i]=ad[i]+'';
}
}
if(t==)
{
for(i=len;i>=;i--)
{
ads[i]=ads[i-];
}
ads[]='';
}
return ;
} int excel()
{
ans[][]='';
ans[][]='';
char sum[];
int n;
int i,j;
char t[];
memset(sum,,sizeof(sum));
for(i=;i<=;i++)
{
for(j=i;j>;j--)
{
multiply(ans[i-j],ans[j-]);
additive(mus,sum);
strcpy(sum,ads);
}
strcpy(ans[i],sum);
memset(sum,,sizeof(sum));
}
jc[][]='';
for(i=;i<;i++)
{
memset(t,,sizeof(t));
if(i>=)
{
t[]=i/+'';
t[]=i%+'';
}
else
{
t[]=i+'';
}
multiply(jc[i-],t);
strcpy(jc[i],mus);
}
multiply(jc[],"");
strcpy(jc[],mus);
for(i=;i<=;i++)
{
multiply(ans[i],jc[i]);
strcpy(res[i],mus);
//cout<<"res["<<i<<"]="<<res[i]<<endl;
}
return ;
} int main()
{
int n;
excel();
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
cout<<res[n]<<endl;
}
return ;
}
HDU 1131 Count the Trees 大数计算的更多相关文章
- Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)
Count the Trees Another common social inability is known as ACM (Abnormally Compulsive Meditation) ...
- HDU 1131 Count the Trees
卡特兰数再乘上n的阶乘 #include<iostream> #include<cstdio> using namespace std; #define base 10000 ...
- hdu 1130,hdu 1131(卡特兰数,大数)
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- zjuoj 3602 Count the Trees
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seco ...
- Count the Trees[HDU1131]
Count the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1023 卡特兰数《 大数》java
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- js 大数计算
js 大数计算 原理 JavaScript 安全整数 是 -253-1 ~ 253-1 ,即: -9007199254740991 ~ 9007199254740991; 换句话说,整数超过这个范围就 ...
- HDU 4588 Count The Carries 计算二进制进位总数
点击打开链接 Count The Carries Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU 1042 大数计算
这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就MLE 这里写好大数后,每次输入一个n,然后再老老实实一个个求阶层就好 java代码: /** * @(#)Main. ...
随机推荐
- Jenkins+MSbuild+SVN实现dotnet持续集成 快速搭建持续集成环境
Jenkins是一个可扩展的持续集成引擎,Jenkins非常易于安装和配置,简单易用,下面开始搭建dotnet持续集成环境 一.准备工作 1.系统管理-->管理插件-->可选插件中找到MS ...
- WPF自定义控件与样式(15)-终结篇 & 系列文章索引 & 源码共享
系列文章目录 WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与样式(3)-TextBox & Ric ...
- hibernate(八)一对多关联
一.一对多单向关良 一对多单向关联与多对一相似 假设一个组有多个用户,即一(Group)对多(User) 需要在Group类中添加一个User类的Set集合(数据库中的用户不可能是重复的,所以要用Se ...
- javascript类型系统——字符串String类型
× 目录 [1]定义 [2]引号 [3]反斜线[4]特点[5]转字符串 前面的话 javascript没有表示单个字符的字符型,只有字符串String类型,字符型相当于仅包含一个字符的字符串 字符串S ...
- SQL 语句中union all和order by同时使用
最近做的一个财物管理系统中查询过期或逾期的存储过程,返回 “财物所属的案件名称”,“财物名称”,“财物编号”,“过期或逾期时间”(超期或逾期前7天开始预警). 遇到“union all ...
- 拓扑排序(三)之 Java详解
前面分别介绍了拓扑排序的C和C++实现,本文通过Java实现拓扑排序. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑排序的代码说明 4. 拓扑排序的完整源码和测试程序 转载请注明出处 ...
- AngularJS in Action读书笔记6(实战篇)——bug hunting
这一系列文章感觉写的不好,思维跨度很大,原本是由于与<Angularjs in action>有种相见恨晚而激发要写点读后感之类的文章,但是在翻译或是阐述的时候还是会心有余而力不足,零零总 ...
- git命令使用记录
github网络连接稍微有些慢,这里使用coding做测试: 首先要分两种情况,一种是直接clone远程库,一种是将本地的代码push到远程库.先说clone到本地 1.clone 通常是我们clon ...
- 30天C#基础巩固----查找XML文件元素
一:XML文档 了解xml文档. 利用代码来创建XML文档. //引用命名空间+using System.Xml; XmlDocument xdoc=new XmlDocument(); XmlDec ...
- Linux - 修复Ubuntu错误“System program problem detected”
The error "System program problem detected" comes up when a certain application crashes. U ...