Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string subtract(string a, string b) {
- string ans="";
- reverse(a.begin(), a.end());
- reverse(b.begin(), b.end());
- int carry=0;
- for (long long i=0; i<a.length(); i++) {
- int sub;
- if (i>=b.length()) sub=a[i]-'0'-carry;
- else sub=a[i]-b[i]-carry;
- if (sub<0) {
- sub+=10;
- carry=1;
- }
- else carry=0;
- ans.push_back(sub+'0');
- }
- while (ans.back()=='0') ans.pop_back();
- reverse(ans.begin(), ans.end());
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment