由二项式系数表示的第n个加泰罗尼亚数由下式计算
(n + k)/ k,其中k从2到n变化并且n≥0。即
Cn =(2n)!/(((n + 1)!n!)
public class CatalanNumbers { public static long fact(int i) { if(i <= 1) { return 1; } return i * fact(i - 1); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("输入数字:"); int num = sc.nextInt(); //(2n)!/(n + 1)!* n! for(int i = 0; i<=num; i++) { long Cn = (fact(2*i))/(fact(i+1)*fact(i)); System.out.println("C"+i+": "+Cn); } } }
输出结果
输入数字: 7 C0: 1 C1: 1 C2: 2 C3: 5 C4: 14 C5: 42 C6: 132 C7: 429