POJ2084—Game of Connections(c++高精度)

Game of Connections

description:

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

题意:

给你一个数n,然后从1到2n这么2n个数围成一圈,每两个数连成一条线即可,但是要求线不能相交。


思路:

  • 一开始没看出来是卡特兰数。是这样想的,我从1开始连,那么1有多少种连法呢。1能连2,1能连4,1能连6…..1不能连3因为连3后2就是一个单个数字那么肯定有线要穿过1与3的连线,以此类推。

  • 那么比如我们有6个数,1连到2以后,剩下数全部在连线的一侧,可以看做不管1和2了,是新的4个数来连线,再比如1连4,那么就分成两部分,一侧是2和3两个数,另一侧是5和6两个数,那么很容易就能想到递推式。f(n) = f(j) * f(i-j-1) ; 1<=n<=100,0<=j<=i-1;

  • 同时还有一个问题就是高精度,这个题到后面的时候,数字很大,用大数模板。


AC代码



    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <string>
    using namespace std;

    const int maxn = 200;
    struct bign {
        int len, s[maxn];

        bign() {
            memset(s, 0, sizeof(s));
            len = 1;
        }

        bign(int num) {
            *this = num;
        }

        bign(const char* num) {
            *this = num;
        }

        bign operator =(int num) { //直接以整数赋值
            char s[maxn];
            sprintf(s, "%d", num);
            *this = s;
            return *this;
        }

        bign operator =(const char* num) { //以字符串赋值
            len = strlen(num);
            for(int i = 0; i < len; i++)
                s[i] = num[len - i - 1] - '0';
            return *this;
        }

        string str() const { //将bign转化成字符串
            string res = "";
            for(int i = 0; i < len; i++)
                res = (char) (s[i] + '0') + res;
            if(res == "")
                res = "0";
            return res;
        }

        bign operator +(const bign& b) const { //重载+号运算
            bign c;
            c.len = 0;
            for(int i = 0, g = 0; g || i < max(len, b.len); i++) {
                int x = g;
                if(i < len) x += s[i];
                if(i < b.len) x += b.s[i];
                c.s[c.len++] = x % 10;
                g = x / 10;
            }
            return c;
        }

        void clean() { //去掉前到0
            while(len > 1 && !s[len - 1])
                len--;
        }

        bign operator *(const bign& b) { //重载*号运算
            bign c;
            c.len = len + b.len;
            for(int i = 0; i < len; i++)
                for(int j = 0; j < b.len; j++)
                    c.s[i + j] += s[i] * b.s[j];
            for(int i = 0; i < c.len - 1; i++) {
                c.s[i + 1] += c.s[i] / 10;
                c.s[i] %= 10;
            }
            c.clean();
            return c;
        }

        bign operator -(const bign& b) { //重载-号运算
            bign c;
            c.len = 0;
            for(int i = 0, g = 0; i < len; i++) {
                int x = s[i] - g;
                if(i < b.len)
                    x -= b.s[i];
                if(x >= 0)
                    g = 0;
                else {
                    g = 1;
                    x += 10;
                }
                c.s[c.len++] = x;
            }
            c.clean();
            return c;
        }

        bool operator <(const bign& b) const { //重载<号运算
            if(len != b.len)
                return len < b.len;
            for(int i = len - 1; i >= 0; i--)
                if(s[i] != b.s[i])
                    return s[i] < b.s[i];
            return false;
        }

        bool operator >(const bign& b) const { //重载>号运算
            return b < *this;
        }

        bool operator <=(const bign& b) { //重载<=号运算
            return !(b > *this);
        }

        bool operator ==(const bign& b) { //重载>=号运算
            return !(b < *this) && !(*this < b);
        }

        bign operator +=(const bign& b) { //重载+=号运算
            *this = *this + b;
            return *this;
        }
    };

    istream& operator >>(istream &in, bign& x) { //重载输入运算符
        string s;
        in >> s;
        x = s.c_str();
        return in;
    }

    ostream& operator <<(ostream &out, const bign& x) { //重载输出运算符
        out << x.str();
        return out;
    }

    bign a[103];

    int main()
    {
        a[0] = 1;
        a[1] = 1;
        a[2] = 2;
        a[3] = 5;

        for(int i = 4;i<=100;i++)
        {   
            for(int j = 0;j < i; j++)
            {
                a[i] += a[j] * a[i-1-j];
            }
        }

        int n;
        while(scanf("%d",&n)!=EOF && n != -1)
        {
            cout<<a[n]<<endl;
        } 
        return 0;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值