Train Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5295 Accepted Submission(s): 2869
import java.util.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
BigInteger[] a = new BigInteger[110];
a[0] = BigInteger.ONE;
a[1] = BigInteger.valueOf(1);
for(int i =2;i <= 100; i++)
{
a[i] = a[i-1].multiply(BigInteger.valueOf(4 * i -2)).divide(BigInteger.valueOf(i + 1));
}
Scanner in = new Scanner(System.in);
int n;
while(in.hasNext())
{
n = in.nextInt();
System.out.println(a[n]);
}
}
}
//*******************************
//打表卡特蘭數
//第 n個 卡特蘭數存在a[n]中,a[n][0]表示長度;
//注意數是倒着存的,個位是 a[n][1] 輸出時注意倒過來。
//*********************************
#include<cstdio>
using namespace std;
int a[110][300];
int n;
void catalan()
{
a[1][0]=1;
a[1][1]=1;
a[2][0]=1;
a[2][1]=2;
for(int i=3;i<=100;i++)
{
int len=a[i-1][0];
int carry=0;
for(int j=1;j<=len;j++)//進行乘法
{
int temp=a[i-1][j]*(4*i-2)+carry;
carry=temp/10;
a[i][j]=temp%10;
}
while(carry)//處理餘數
{
a[i][++len]=carry%10;
carry/=10;
}
for(int j=len;j>=1;j--)//進行除法
{
int temp=a[i][j];
a[i][j]=(temp+carry*10)/(i+1);
carry=(temp+carry*10)%(i+1);
}
while(!a[i][len])len--;//消去前導零
a[i][0]=len;
}
}
int main()
{
catalan();
while(~scanf("%d",&n))
{
for(int i=a[n][0];i>=1;i--)
printf("%d",a[n][i]);
printf("\n");
}
return 0;
}