Train Problem II

20 篇文章 0 订阅
3 篇文章 0 订阅

Train Problem II

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 3
描述
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.
输入
The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.
输出
For each test case, you should output how many ways that all the trains can get out of the railway.
(The result will be very large, so you may not process it by 32-bit integers.)
样例输入
1
2
3
10
样例输出
1
2
5

16796

思路:

卡特兰数的应用,以前都是用栈加深搜写的。。。没想到套个公式就行。。。

//train problem II
//卡特兰数求法:Kn = [(n+2)*(n+3)*...*(2*n)] / [1*2*...*n]
import java.math.BigInteger;
import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		BigInteger a, b, c;
		int n, i;
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			a = BigInteger.ONE;
			b = BigInteger.ONE;
			c = BigInteger.ONE;
			n = sc.nextInt();
			for(i = 1; i <= n; i++){
				a = a.multiply(BigInteger.valueOf(i));
			}
			for(i = n+2; i <= 2*n; i++)
			{
				b = b.multiply(BigInteger.valueOf(i));
			}
			c = b.divide(a);
			System.out.println(c);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值