Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int NMAX = 1000;
  5. bool strgreater(string a,string b){
  6.     if(a.size() > b.size()) return true;
  7.     else if(a.size() < b.size()) return false;
  8.     bool fl = false;
  9.     for(int i = 0; i < a.size(); i++){
  10.         if(a[i] - '0' == b[i] - '0') continue;
  11.         if(a[i] - '0' > b[i] - '0') return true;
  12.     }
  13.     return false;
  14. }
  15. int main(int argc,char* argv[]) {
  16.  
  17. ios_base::sync_with_stdio(false);
  18. cin.tie(NULL);
  19. ///
  20. string a,b;
  21. cin >> a >> b;
  22. bool rev = false;
  23. if(a == "0" && b == "0" || a == "-0" && b == "0" || a == "0" && b == "-0" || a == "-0" && b =="-0" || a == b){
  24.     cout << 0;
  25.     return 0;
  26. }
  27.  
  28. if(strgreater(b,a))
  29. {
  30.     swap(a,b);
  31.     rev = true;
  32. }
  33.  
  34. vector<int> A(NMAX,0),B(NMAX,0),C(NMAX,0),ANS(NMAX,0);
  35. int j = 0;
  36. for(int i = a.size() - 1; i >=0; i--) {
  37.     A[j] = a[i]-'0';
  38.     j++;
  39. }
  40. j = 0;
  41. for(int i = b.size() - 1; i >=0; i--) {
  42.     B[j] = b[i]-'0';
  43.     j++;
  44. }
  45.  
  46. for(int i = 0; i <= NMAX; i++){
  47.     int f;
  48.     int s = A[i] - B[i] - C[i];
  49.     if(s < 0){
  50.         ANS[i] = A[i] + 10 - B[i] - C[i];
  51.         C[i+1] = 1;
  52.     }
  53.     else ANS[i] = s;
  54. }
  55.  
  56. int f=NMAX;
  57. while(ANS[f] == 0){
  58. f--;   
  59. }
  60. if(rev) cout<<"-";
  61. for(int i = f; i >=0; i--) cout << ANS[i];
  62. ///
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement