fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define el "\n"
  4. #define ll long long
  5. #define ull unsigned long long
  6. #define se second
  7. #define fi first
  8. #define be begin()
  9. #define en end()
  10. #define Faster cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
  11.  
  12. int main()
  13. {
  14. Faster;
  15. string a, b; cin >> a >> b;
  16. int ok = 0;
  17. string s = "";
  18. if(a.size() < b.size())
  19. {
  20. swap(a,b);
  21. ok = 1;
  22. }
  23. if(a.size() == b.size() && a < b)
  24. {
  25. swap(a,b);
  26. ok = 1;
  27. }
  28. string tmp = "";
  29. for(int i = 0; i < a.size() - b.size(); i++)
  30. {
  31. tmp += '0';
  32. }
  33. b= tmp + b;
  34. int du = 0;
  35. for(int i = a.size() - 1; i >= 0; i--)
  36. {
  37. int x = a[i] - '0';
  38. int y = b[i] - '0' + du;
  39. if(x < y)
  40. {
  41. x += 10;
  42. du = 1;
  43. }
  44. else
  45. {
  46. du = 0;
  47. }
  48. s += to_string(x - y);
  49. }
  50. if(s.size() > 1 && s.back() == '0') s.pop_back();
  51. if(ok == 1) cout << "-";
  52. reverse(s.begin(), s.end());
  53. cout << s;
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0.01s 5284KB
stdin
10
999999
stdout
-999989