A + B Problem II

May 6, 2015


冰洞外的滑雪者

```cpp #include #include

using namespace std;

int main() { int cnt,a1,b1,res; string a,b,c; // freopen(“t.in”,”r”,stdin); // freopen(“t.out”,”w”,stdout); cin»cnt; for ( int i = 0; i<cnt; i++) { a.clear(); b.clear(); c.clear(); cin»a»b; int lenA = a.size(); int lenB = b.size(); int n = (lenA > lenB ? lenB : lenA); res = 0; if (lenA >= lenB) { c = a; } else { c = b; } int lenC = c.size(); for (int j = 1; j <= n; j++ ) { a1 = a[lenA - j] - ‘0’ + res; b1 = b[lenB - j] - ‘0’; res = a1 + b1; c[lenC - j] = res % 10 + ‘0’; res = res / 10; } for ( int j = lenC - n - 1; j >=0 ; j–) { a1 = c[j] - ‘0’ + res; c[j] = a1 % 10 +’0’; res = a1 / 10; if (res == 0) break; } cout«“Case “«i+1«”:”«endl«a«” + “«b«” = “; if (res > 0) { cout«char(res + ‘0’); } cout«c; if (i < cnt - 1) cout«endl;

	cout<<endl;
}
// fclose(stdin);
// fclose(stdout);
return 0; }