[大数] HDU 1250 - Hat's Fibonacci

2 篇文章 0 订阅

直接按题意,用数组模拟大数相加即可。。注意进位情况。

一开始用int二维数组,MLE了。。然后改成char数组过了。。

后来想了一下,用int数组也可做,把int每位存储由10进制编程100000进制。然后就可以把数组大小减小五倍。

做法一:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <malloc.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int ch[7043][502];
int main()
{
    //freopen("test0.in", "r", stdin);
    //freopen("test01.out", "w", stdout);
    //srand(time(NULL));
    int N;
    ch[0][500] = ch[1][500] = ch[2][500] = ch[3][500] = 1;
    for(int i = 4; i <= 7042; i++)
    {
        for(int j = i-4; j < i; ++j)
        {
            for(int k = 500; k >= 0; k--)
            {
                ch[i][k] += ch[j][k];
                if(ch[i][k] > 99999)
                {
                    ch[i][k-1] += ch[i][k] / 100000;
                    ch[i][k] %= 100000;
                }
            }
        }
    }
    while(~scanf("%d", &N))
    {
        int j;
        for(j = 0; j <= 600; j++)
        {
            if(ch[N-1][j] != 0)
            {
                break;
            }
        }
        printf("%d", ch[N-1][j++]);
        while(j <= 500)
        {
            printf("%05d", ch[N-1][j++]);
        }
        puts("");
    }
    return 0;
}

做法二:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
char ch[7043][2009];
int main()
{
    //freopen("test0.in", "r", stdin);
    //freopen("test0.out", "w", stdout);
    //srand(time(NULL));
    int N;
    ch[0][2008] = ch[1][2008] = ch[2][2008] = ch[3][2008] = 1;
    for(int i = 4; i <= 7042; i++)
    {
        for(int j = i-4; j < i; ++j)
        {
            for(int k = 2008; k >= 0; k--)
            {
                ch[i][k] += ch[j][k];
                if(ch[i][k] > 9)
                {
                    ch[i][k-1] += ch[i][k] / 10;
                    ch[i][k] %= 10;
                }

            }
        }
    }
    while(~scanf("%d", &N))
    {
        int j;
        for(j = 0; j <= 2008; j++)
        {
            if(ch[N-1][j] != 0)
            {
                break;
            }
        }
        while(j <= 2008)
        {
            printf("%d", (int)ch[N-1][j++]);
        }
        puts("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值