lina_os

Untitled

Apr 4th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. string subtract(string a, string b) {
  2. string ans="";
  3. reverse(a.begin(), a.end());
  4. reverse(b.begin(), b.end());
  5. int carry=0;
  6. for (long long i=0; i<a.length(); i++) {
  7. int sub;
  8. if (i>=b.length()) sub=a[i]-'0'-carry;
  9. else sub=a[i]-b[i]-carry;
  10. if (sub<0) {
  11. sub+=10;
  12. carry=1;
  13. }
  14. else carry=0;
  15. ans.push_back(sub+'0');
  16. }
  17. while (ans.back()=='0') ans.pop_back();
  18. reverse(ans.begin(), ans.end());
  19. return ans;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment