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

HDU-1715

时间:2014-08-28 11:05:09      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   ar   2014   div   cti   

/********************************************************************
@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

标签:style   blog   color   io   for   ar   2014   div   cti   

原文地址:http://www.cnblogs.com/roronoa-zoro-zrh/p/3940893.html

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