注解
1、卡塔兰数(Catalan数):令h(0)=1,h(1)=1,catalan数满足递推式。h(n)= h(0)*h(n-1)+h(1)*h(n-2) + … + h(n-1)h(0) (n>=2)。
2、Java大数BigInteger的应用。
代码
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigInteger[] catalan = new BigInteger[101];
catalan[0] = BigInteger.ONE;
catalan[1] = BigInteger.ONE;
for(int i=2; i<catalan.length; i++){
catalan[i] = BigInteger.ZERO;
for(int j=0; j<i; j++){
catalan[i] = catalan[i].add(catalan[j].multiply(catalan[i-1-j]));
}
}
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
System.out.println(catalan[n]);
}
}
}