码迷,mamicode.com
首页 > 其他好文 > 详细

hdu_1023

时间:2015-01-27 00:06:47      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

// hdu 1023
// number theory - catalan number
// Jan.26 2015

#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAXN 60

struct rev
{
    int num[MAXN];
    int l;
    // when the struct is constructed 
    // it‘s l value must be 1
    // ---- it‘s l must be 1 
    // ---- the num is 0 
    rev(int _l = 1)
    {
        memset(num, 0, sizeof(num));
        l = _l;
    }
}ca[101];

// two big number plus
rev add(rev a, rev b)
{
    rev s;
    s.l = std::max(a.l, b.l);
    int carry = 0;
    for(int i = 1; i <= s.l; ++i){
        int t = carry + a.num[i] + b.num[i];
        carry = t / 10;
        s.num[i] = t % 10;
    }
    if(carry){
        ++s.l;
        s.num[s.l] = 1;
    }
    return s;
}

// big number multiply big number
rev multiply(rev a, rev b)
{
    rev s;
    memset(s.num, 0, sizeof(s.num));
    s.l = 1;
    s.num[1] = 0;
    for(int i = 1; i <= a.l; ++i){
        rev temp;

        // Every digit of a should do a multiplication to b
        // and the i-th digit of a should have i-1 zero before the first zero-none digit 
        // for instance .
        // 45 * 123
        // the first digit of a is 5
        // 5 * 123 = 615 
        // 615 should puls no zero before
        // the second digit of b is 4
        // 4 * 123 = 492
        // but 492 should have 1 zero before 2
        // so the product of the multiplication is 4920
        // the result of 45*123 is 4920 + 615 =5535
        for(int pre_zero = 1; pre_zero < i; ++pre_zero)
            temp.num[pre_zero] = 0;
        temp.l = i-1;

        int multiplier_int = a.num[i];
        int carry = 0;
        for(int j = 1; j <= b.l; ++j){
            int t = multiplier_int * b.num[j] + carry;
            carry = t / 10;
            temp.num[++temp.l] = t % 10;
        }

        if(carry){
            ++temp.l;
            temp.num[temp.l] = carry;
        }
        s = add(s, temp);
    }
    return s;
}

void pre_do()
{
    memset(ca, 0, sizeof(ca));
    ca[0].num[1] = 1; ca[0].l = 1;
    ca[1].num[1] = 1; ca[1].l = 1;
    for(int i = 2; i <= 100; ++i){
        ca[i].num[1] = 0; ca[i].l = 1;
        rev temp;
        for(int j = 0; j < i; ++j){
            temp = multiply(ca[j], ca[i-j-1]);
            ca[i] = add(ca[i], temp);
        }

    }
}


int main(int argc, char const *argv[])
{
    pre_do();
    int n;
    while(~scanf("%d", &n)){
        for(int i = ca[n].l; i >= 1; --i)
            printf("%d", ca[n].num[i]);
        printf("\n");
    }
    // for(int i = 0; i <= 100; ++i){
    //     for(int j = ca[i].l; j >= 1; --j)
    //         printf("%d", ca[i].num[j]);
    //     printf("\n");
    // }
    return 0;
}

 

hdu_1023

标签:

原文地址:http://www.cnblogs.com/takeoffyoung/p/4251542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!