HDU-1250

/********************************************************************
@file     Main_practise.cpp
@date     2014-8-29
@author   Tiger
@brief    Hat‘s Fibonacci
@details  大数
********************************************************************/
#include <cstdio>
#include <string>
#include <algorithm>

const int SIZE = 2006;
const int MAX = 100000;
std::string g_Records[MAX];

std::string Add(const std::string& strA, const std::string& strB);
void CalFab(void);

int main(int argc, const char* argv[])
{
    CalFab();

    int nNum = 0;
    while (scanf("%d", &nNum) != EOF)
    {
        printf("%s\n", g_Records[nNum].c_str());
    }

    return 0;
}

std::string Add(const std::string& strA, const std::string& strB)
{
    char strTemp[SIZE] = {0};

    for (int i=strA.size()-1, m=0; i>=0; --i, ++m)
    {
        strTemp[m] =  strA.at(i) - ‘0‘;
    }
    for (int i=strB.size()-1, n=0; i>=0; --i, ++n)
    {
        strTemp[n] += strB.at(i) - ‘0‘;
    }

    int nMax = std::max(strA.size(), strB.size());
    std::string strSum(strTemp, strTemp+nMax);
    strSum.resize(strSum.size()+1);

    for (unsigned int i=0; i<strSum.size()-1; ++i)
    {
        strSum[i+1] += strSum[i]/10;
        strSum[i] = strSum[i]%10 + ‘0‘;
    }
    strSum[strSum.size()-1] = strSum[strSum.size()-1]%10 + ‘0‘;

    for (unsigned int i=0; i<strSum.size()/2; ++i)
    {
        std::swap(strSum[i], strSum[strSum.size()-1-i]);
    }

    if (strSum.at(0) == ‘0‘)
    {
        strSum.erase(strSum.begin());
    }

    return strSum;
}

void CalFab(void)
{
    g_Records[1] = "1";
    g_Records[2] = "1";
    g_Records[3] = "1";
    g_Records[4] = "1";

    std::string str1 = g_Records[1], str2 = g_Records[2];
    std::string str3 = g_Records[3], str4 = g_Records[4];
    for (int i=5; i<MAX; ++i)
    {
        g_Records[i] = Add(str1, str2);
        g_Records[i] = Add(g_Records[i], str3);
        g_Records[i] = Add(g_Records[i], str4);
        if (g_Records[i].size() > SIZE)
        {
            break;
        }
        str1 = str2;
        str2 = str3;
        str3 = str4;
        str4 = g_Records[i];
    }
}
时间: 2024-10-10 11:10:13

HDU-1250的相关文章

hdu 1250 树形DP

Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-07-27) Description There is going to be a party to celebrate the 80-th Anniversary of the Ural St

HDU 1250 Hat&#39;s Fibonacci(Java大数相加)+讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n -

hdu 1250 Hat&amp;#39;s Fibonacci

点击此处就可以传送hdu 1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) You

hdu 1250 Hat&#39;s Fibonacci

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9677 Accepted Submission(s): 3210 Problem Description A Fibonacci sequence is calculated by adding the previous two members the seque

hdu 1250 Hat&#39;s Fibonacci(高精度数)

//  继续大数,哎.. Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your tas

HDOJ/HDU 1250 Hat&#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

HDU 1250: Hat&#39;s Fibonacci

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6925    Accepted Submission(s): 2279 Problem Description A Fibonacci sequence is calculated by adding the previous two members th

HDU 1250 大数加法

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6948    Accepted Submission(s): 2285 Problem Description A Fibonacci sequence is calculated by adding the previous two members the

hdu 1250 Hat&#39;s Fibonacci (大数相加)

//a[n]=a[n-1]+a[n-2]+a[n-3]+a[n-4]; # include <stdio.h> # include <algorithm> # include <string.h> # include <iostream> using namespace std; int a[10000][260]={0}; //每个元素可以存储8位数字,所以2005位可以用260个数组元素存储. int main() { int i,j,n; a[1][0

HDU 1250 Hat&#39;s Fibonacci(大数相加)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12952    Accepted Submission(s): 4331 Problem Description A Fibonacci sequence