用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]);
		}
	}
	

}

猜你喜欢

转载自blog.csdn.net/JKdd123456/article/details/83304115