【HDU - 1134 】Game of Connections(JAVA大数加法,卡特兰数)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83684304

题干:

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号顶点开始找,发现是有递推规律的、、然后longlong了好几发还以为是思路不对,,一看输入70的时候是个负数、、所以果断转Java大整数类、、比赛的时候有事出去了就没提交,,回来一发AC2333、、Java大法好啊、、

  查题解后发现是卡特兰数?这个不是处理栈的问题的吗、、没深入学习过、、

AC代码:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void  main(String[] args) {
        Scanner cin = new Scanner(System.in);
        BigInteger[] a = new BigInteger[205];
        a[1]=a[0]=BigInteger.valueOf(1);
        for(int i = 2; i<=200; i++) a[i]=BigInteger.valueOf(0);
        for(int i = 2; i<=102; i++) {
            for(int j = 0; j<i; j++) {
                BigInteger tmp = BigInteger.valueOf(0);
                tmp=a[j];
                tmp=tmp.multiply(a[i-j-1]);
                //System.out.println(a[i]);
                a[i]=a[i].add(tmp);

            }
        }
        while(cin.hasNext()) {
            int x = cin.nextInt();
            if(x == -1) break;
            System.out.println(a[x]);
        }

    }
}

总结:用Java大数的数组的时候一定别忘了实例化对象数组的时候要附上初值,不然指向NULL。。就没法进行操作了、。

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83684304
今日推荐