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