Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using  namespace  std;
  4. string FillWithZeros(int nums)
  5. {
  6.     string text = "";
  7.     for(int counter = 0; counter < nums; counter++)
  8.     {
  9.         text += "0";
  10.     }
  11.     return text;
  12. }
  13.  
  14. int main()
  15. {
  16.     string firstNum, secondNum;
  17.     cin>>firstNum;
  18.     cin>>secondNum;
  19.     bool secondIsBigger = false;
  20.     if (secondNum.length() > firstNum.length())
  21.     {
  22.         string x = firstNum;
  23.         firstNum = secondNum;
  24.         secondNum = x;
  25.         secondIsBigger = true;
  26.     }
  27.     if (firstNum.length() > secondNum.length())
  28.     {
  29.         int diff = firstNum.length() - secondNum.length();
  30.         secondNum = FillWithZeros(diff) + secondNum;
  31.     }
  32.     else if (firstNum.length() < secondNum.length())
  33.     {
  34.         int diff = secondNum.length() - firstNum.length();
  35.         firstNum = FillWithZeros(diff) + firstNum;
  36.     }
  37.  
  38.     string result = "";
  39.  
  40.     if (secondNum.length() == firstNum.length())
  41.     {
  42.         for(int index = 0; index < firstNum.length(); index++)
  43.         {
  44.             if (secondNum[index] - 48 > firstNum[index] - 48)
  45.             {
  46.                 string x = firstNum;
  47.                 firstNum = secondNum;
  48.                 secondNum = x;
  49.                 secondIsBigger = true;
  50.             }
  51.             else if (secondNum[index] - 48 < firstNum[index] - 48)
  52.             {
  53.                 break;
  54.             }
  55.         }
  56.     }
  57.     for(int index = firstNum.length() - 1; index >= 0; index--)
  58.     {
  59.         int firstNumDigit = (int)firstNum[index] - 48;
  60.         int secondNumDigit = (int)secondNum[index] - 48;
  61.         if (firstNumDigit < secondNumDigit)
  62.         {
  63.             firstNum[index - 1]--;
  64.             firstNumDigit += 10;
  65.         }
  66.         int diff = firstNumDigit - secondNumDigit;
  67.         result = (char)(diff + 48) + result;
  68.     }
  69.  
  70.     while(true)
  71.     {
  72.         if (result[0] == '0')
  73.         {
  74.             result = result.substr(1);
  75.         }
  76.         else
  77.         {
  78.             break;
  79.         }
  80.     }
  81.  
  82.     if (secondIsBigger)
  83.     {
  84.         result = "-" + result;
  85.     }
  86.  
  87.     cout<<result<<endl;
  88. return 0;
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement