又是一道大数相加的题目,直接模板或者Java都可以水过了。
循环相加33次就可以了,计算出A99是第几个,准确输出答案。
#include <stdio.h> #include <string> #include <algorithm> using std::string; const int MAX_B = 5120; char buf[MAX_B]; int id = 0, len = 0; inline char getFromBuf() { if (id >= len) { len = fread(buf, 1, MAX_B, stdin); id = 0; } return buf[id++]; } void getIntFromBuf(string &n) { char a = getFromBuf(); while ((a == ' ' || a == '\n') && len) a = getFromBuf(); n.clear(); while ((a != ' ' && a != '\n') && len)//老是写&&,错成|| { n.push_back(a); a = getFromBuf(); } } string operator+(string &a, string &b) { string c; int N1 = (int)a.size(), N2 = (int)b.size(); int carry = 0; for (int i = N1-1, j = N2-1; i>=0 || j>=0 || carry; i--, j--) { int an = i>=0? a[i]-'0' : 0; int bn = j>=0? b[j]-'0' : 0; int sum = an + bn + carry; carry = sum / 10; c.push_back(sum % 10 + '0'); } reverse(c.begin(), c.end()); return c; } int main() { string a1, a2, a3; while (true) { getIntFromBuf(a1); if (len == 0) break; getIntFromBuf(a2); getIntFromBuf(a3); for (int i = 0; i < 33; i++) { string a = a2 + a3; a1 = a1 + a; if (i == 32) break; a2 = a1 + a; a3 = a3 + a1; a3 = a3 + a2; } puts(a1.c_str()); } return 0; }
POJ 3982 序列 大数题解,布布扣,bubuko.com
原文地址:http://blog.csdn.net/kenden23/article/details/36876845