首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。