1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| string Mysub(string a, string b) { int flag = 0; if(cmp(a, b)<0) { string tmp = a; a = b; b = tmp; flag = 1; } long long len = 0; if (a.length() > b.length()) { len = a.length() + 10; } else { len = b.length() + 10; } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); string ans(len, '0'); for (int i = 0; i < a.length(); i++) { ans[i] = a[i]; } int tmp = 0; for (int i = 0; i < len; i++) { if (i < b.length()) { tmp += (ans[i] - '0') - (b[i] - '0'); ans[i] = Mod(tmp,10) + '0'; tmp = (tmp - 9) / 10; } else { tmp += (ans[i] - '0'); ans[i] = Mod(tmp, 10) + '0'; tmp = (tmp - 9) / 10; } } reverse(ans.begin(), ans.end()); if (ans.find_first_not_of('0') != string::npos) { if (flag == 1) { ans[ans.find_first_not_of('0') - 1] = '-'; } ans = ans.substr(ans.find_first_not_of('0')); return ans; } else { return "0"; } }
|