4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: string add(const string& a, const string& b) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int n = max(a.size(), b.size()), carry=0; 4fd800b3a8 2011-02-23 kinaba: string c(n, '0'); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) { 4fd800b3a8 2011-02-23 kinaba: int x = (a.size()<=i ? 0 : a[a.size()-1-i]-'0') 4fd800b3a8 2011-02-23 kinaba: + (b.size()<=i ? 0 : b[b.size()-1-i]-'0') + carry; 4fd800b3a8 2011-02-23 kinaba: c[n-1-i] = char('0'+x%10); 4fd800b3a8 2011-02-23 kinaba: carry = x/10; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( carry ) c = char('0'+carry)+c; 4fd800b3a8 2011-02-23 kinaba: return c; 4fd800b3a8 2011-02-23 kinaba: }