Game of ConnectionsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5606 Accepted Submission(s): 3126 http://acm.hdu.edu.cn/showproblem.php?pid=1134 Problem Description This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.
Input Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100.
Output For each n, print in a single line the number of ways to connect the 2n numbers into pairs.
Sample Input 2 3 -1
Sample Output 2 5 |
题意:输入n,有2n个点,把他们两两相连且不想交,有几种连法?
思路:画图分析得:1-n:1,2,5,14,42... 是卡特兰数,用公式。
代码:
import java.util.*;
import java.math.*;
public class game {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
while(cin.hasNext()) {
int n=cin.nextInt();
if(n==-1)
break;
BigInteger sum;
BigInteger a=BigInteger.valueOf(1);
BigInteger b=BigInteger.valueOf(2);
if(n==1) {
sum=a;
}
else if(n==2) {
sum=b;
}
else {
sum=b;
for(int i=3;i<=n;i++) {
int c=4*i-2;
BigInteger d=BigInteger.valueOf(c);
sum=sum.multiply(d);
int e=i+1;
BigInteger f=BigInteger.valueOf(e);
sum=sum.divide(f);
}
}
System.out.println(sum);
}
cin.close();
}
}