1. 程式人生 > >用JAVA 實現卡特蘭數

用JAVA 實現卡特蘭數

例1:

B - Game of Connections

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

題意:

給你2n個數,讓這些數按照順序圍成一個圈,任意兩個數可以連線,但是每個數只能連線一次,且連線不能交叉,問你一共有多少種方式。

轉化成卡特蘭數型別中——在圓上選擇2n個點,將這些點成對連線起來使得所得到的n條線段不相交的方法數? 

直接套用卡特蘭數模板

用 java 需要注意的地方:

1、java 定義陣列      BigInteger [ ]a=new BigInteger[110];

2、將int 型的轉化成大數型x = x.multiply(BigInteger.valueOf(y));   //  y 是 int 型的 BigInteger.valueOf  強制轉化  

CODE:

import java.math.*;
import java.util.*;
public class Main {
	public static void main(String[] args) {
		int n,t;
		int i;
		BigInteger x,y;
		Scanner in=new Scanner(System.in);
		BigInteger []a=new BigInteger[110];
		a[0]=BigInteger.valueOf(1);
		a[1]=BigInteger.valueOf(1);
		
		for(i=2;i<101;i++) {
			x=a[i-1];
			x=x.multiply(BigInteger.valueOf(4*i-2));
			y=x.divide(BigInteger.valueOf(i+1));
			a[i]=y;
		}
		
		while(in.hasNext()) {
			n=in.nextInt();
			if(n==-1)
				break;
			System.out.println(a[n]);
		}
	}
	

}

例2:

C - How Many Trees?

Description

A binary search tree is a binary tree with root k such that any node v reachable from its left has label (v) <label (k) and any node w reachable from its right has label (w) > label (k). It is a search structure which can find a node with label x in O(n log n) average time, where n is the size of the tree (number of vertices). Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree?

Input

The input will contain a number 1 <= i <= 100 per line representing the number of elements of the set.

Output

You have to print a line in the output for each entry with the answer to the previous question.

Sample Input

1

2

3

Sample Output

1

2

5

題意:

給n個數字,能構建成多少種二叉排序樹。

卡特蘭數模板

CODE:

import java.math.*;
import java.util.*;
public class Main {
	public static void main(String[] args) {
		int n,t;
		int i;
		BigInteger x,y;
		Scanner in=new Scanner(System.in);
		BigInteger []a=new BigInteger[110];
		a[0]=BigInteger.valueOf(1);
		a[1]=BigInteger.valueOf(1);
		
		for(i=2;i<101;i++) {
			x=a[i-1];
			x=x.multiply(BigInteger.valueOf(4*i-2));
			y=x.divide(BigInteger.valueOf(i+1));
			a[i]=y;
		}
		
		while(in.hasNext()) {
			n=in.nextInt();
			System.out.println(a[n]);
		}
	}
	

}