http://acm.hdu.edu.cn/showproblem.php?pid=1023
题目标题:Train Problem II
题目大意:给出一组数,问这组数出栈的情况有几种
这个题的解是卡特兰数,是一种数列,通项公式为
h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)
在这个题中还需要用到高精度加法和高精度乘法,就当复习高精度了
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
char xx[105][200];
void add(char *a,char *b,char *c){
int aa[200]={0},bb[200]={0},re[200]={0};
for(int i=0;i<strlen(a);i++){
aa[i]=a[strlen(a)-i-1]-'0';
}
for(int i=0;i<strlen(b);i++){
bb[i]=b[strlen(b)-i-1]-'0';
}
int m=strlen(a);
if(strlen(b)>strlen(a)) m=strlen(b);
for(int i=0;i<m;i++){
re[i]=aa[i]+bb[i];
}
for(int i=0;i<m;i++){
if(re[i]>=10){
re[i]-=10;
re[i+1]++;
}
}
if(re[m]==1) m++;
for(int i=m-1;i>=0;i--) c[m-1-i]=(char)(re[i]+'0');
c[m]='\0';
}
void mu(char *a,char *b,char *c){
int aa[200]={0},bb[200]={0},re[200]={0};
for(int i=0;i<strlen(a);i++){
aa[i]=a[strlen(a)-i-1]-'0';
}
for(int i=0;i<strlen(b);i++){
bb[i]=b[strlen(b)-i-1]-'0';
}
for(int i=0;i<strlen(b);i++){
for(int j=0;j<strlen(a);j++){
re[i+j]+=aa[j]*bb[i];
}
}
int m=strlen(a)+strlen(b)-1;
for(int i=0;i<m;i++){
if(re[i]>=10) {
re[i+1]+=re[i]/10;
re[i]=re[i]%10;
}
}
if(re[m]!=0) m++;
for(int i=m-1;i>=0;i--) c[m-1-i]=(char)(re[i]+'0');
c[m]='\0';
}
int main()
{
xx[0][0]='1';
xx[0][1]='\0';
xx[1][0]='1';
xx[1][1]='\0';
for(int i=2;i<=100;i++){
char r1[100],r2[100];
for(int j=0;j<i;j++){
mu(xx[j],xx[i-j-1],r1);
add(xx[i],r1,r2);
strcpy(xx[i],r2);
}
}
int x;
while(cin>>x){
cout<<xx[x]<<endl;
}
return 0;
}