首页 > 代码库 > HDU-1715

HDU-1715

/********************************************************************@file     Main_practise.cpp@date     2014-8-28@author   Tiger@brief    大菲波数********************************************************************/#include <cstdio>#include <map>#include <cstring>#include <string>const int SIZE = 100000;std::string Add(const std::string& strA, const std::string& strB);std::map<int, std::string> g_map;int main(int argc, const char* argv[]){    //freopen("out.txt", "w", stdout);    g_map.insert(std::make_pair<int, std::string>(1, "1"));    g_map.insert(std::make_pair<int, std::string>(2, "1"));    std::string str_1 = g_map[1], str_2 = g_map[2], str_sum;    for (int i=3; i<=1000; ++i)    {        str_sum = Add(str_1, str_2);        g_map.insert(std::make_pair<int, std::string>(i, str_sum));        str_1 = str_2;        str_2 = str_sum;    }    int nTestCases = 0;    scanf("%d", &nTestCases);    while (nTestCases--)    {        int nIn = 0;        scanf("%d", &nIn);        printf("%s\n", g_map[nIn].c_str());    }    return 0;}std::string Add(const std::string& strA, const std::string& strB){    char str[SIZE];    memset(str, \0, SIZE);    for (int i=strA.size()-1, j=0; i>=0; --i, ++j)    {        str[j] =  strA.at(i) - 0;    }    for (int i=strB.size()-1, k=0; i>=0; --i, ++k)    {        str[k] += strB.at(i) - 0;    }    int nMax = strA.size() > strB.size() ? strA.size() : strB.size();    std::string strSum(str, str+nMax+1);    for (unsigned int i=0; i<strSum.size(); ++i)    {        if (strSum[i]/10 > 0)        {            strSum[i+1] += strSum[i]/10;        }        strSum[i] = strSum[i]%10 +0;    }    for (unsigned int i=0; i<strSum.size()/2; ++i)    {        std::swap(strSum.at(i), strSum.at(strSum.size()-i-1));    }    while (strSum.at(0) == 0)    {        strSum.erase(strSum.begin());    }    return strSum;}

 

HDU-1715