2n 個點圍成圓,兩兩連線且線段不相交,求連法總數。等價於凸 (n+2) 邊形三角剖分數,即第 n 個卡特蘭數:
C₀ = 1 Cₙ = Cₙ₋₁ * (4n-2) / (n+1)
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigInteger[] C = new BigInteger[101];
C[1] = BigInteger.ONE;
for (int i = 2; i <= 100; ++i) {
C[i] = C[i-1]
.multiply(BigInteger.valueOf(4*i - 2))
.divide(BigInteger.valueOf(i + 1));
}
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
if (n == -1) break;
System.out.println(C[n]);
}
}
}
BigInteger 自動處理任意精度輸入: 2 3 -1 輸出: 2 5