这道题目ac得好辛苦,不是PE,就是WA,还好最终过了。还是大整数加法的问题。#include <iostream> #include <string> #include <algorithm> using namespace std; class big_int : protected string { friend ostream &operator <<(ostream &os, const big_int &right); friend istream &operator >>(istream &is, big_int &right); public: big_int &operator +=(const big_int &right); bool is_zero() { return at(size() - 1) == '0'; } }; ostream &operator <<(ostream &os, const big_int &right) { int i = right.size(); while(i > 0) { --i; os << right[i]; } return os; } istream &operator >>(istream &is, big_int &right) { string str; is >> str; reverse(str.begin(), str.end()); right.string::operator =(str); return is; } big_int &big_int::operator +=(const big_int &right) { int i = 0; char carry = '0'; char mod = '0'; string &left = *this; while(i<size() && i<right.size()) { mod = (carry - '0' + left[i] - '0' + right[i] - '0') % 10 + '0'; carry = (carry - '0' + left[i] - '0' + right[i] - '0') / 10 + '0'; left[i] = mod; ++i; } const string &rest = right.size() > size()? right : *this; resize(rest.size()); while(i<rest.size()) { mod = (carry - '0' + rest[i] - '0') % 10 + '0'; carry = (carry - '0' + rest[i] - '0') / 10 + '0'; left[i] = mod; ++i; } if(carry != '0') append(1, carry); return *this; } int main() { int cases; big_int sum; big_int add; cin >> cases; for(int i=0; i<cases; ++i) { cin >> sum; while(1) { if(sum.is_zero()) break; cin >> add; if(add.is_zero()) break; sum += add; } cout << sum << (i == cases-1? "/n" : "/n/n"); } return 0; }