题目链接:http://poj.org/problem?id=2084
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.
It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?
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
题目翻译:
这是一个小而古老的游戏。你应该写下数字1,2,3,....,2n - 1,2n连续在地面上顺时针顺序形成一个圆,然后,绘制一些直线段,将它们连接到数字对。每个数字必须完全相互连接。
并且,不允许两个线段相交。
这还是个简单的游戏,不是吗?但是,在写下2n个数字后,你能告诉我有多少不同的方式,你可以连接数字成对?生活更艰难,对吧?
输入
输入文件的每一行都将是单个正数 n,但最后一行除外,即数字 -1。
您可以假定 1 <= n <= 100。
输出
对于每个 n,在一行中打印将 2n 数字连接到对的方法数。
题解:
可以推出来这是一个卡特兰数,但是因为数比较大,所以要用高精度,我感觉麻烦,就用了java的BigInteger来写。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main{
static double mod=10e9+7;
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static Scanner scanner=new Scanner(System.in);
public static void main(String[] args)throws IOException{
BigInteger num[]=new BigInteger[101];
num[0]=BigInteger.ONE;
num[1]=BigInteger.ONE;
for(int i=2;i<101;i++)
num[i]=num[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
while(scanner.hasNextInt()) {
int n=scanner.nextInt();
if(n==-1) break;
System.out.println(num[n]);
}
}
}