Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string FillWithZeros(int nums)
- {
- string text = "";
- for(int counter = 0; counter < nums; counter++)
- {
- text += "0";
- }
- return text;
- }
- int main()
- {
- string firstNum, secondNum;
- cin>>firstNum;
- cin>>secondNum;
- bool znak=false;
- if (firstNum.length() > secondNum.length())
- {
- int diff = firstNum.length() - secondNum.length();
- secondNum = FillWithZeros(diff) + secondNum;
- }
- else if (firstNum.length() < secondNum.length())
- {
- int diff = secondNum.length() - firstNum.length();
- firstNum = FillWithZeros(diff) + firstNum;
- swap(firstNum,secondNum);znak=true;
- }
- else if(firstNum<secondNum){ swap(firstNum,secondNum);znak=true;}
- string result = "";
- for(int index = firstNum.length() - 1; index >= 0; index--)
- {
- int firstNumDigit = (int)firstNum[index] - 48;
- int secondNumDigit = (int)secondNum[index] - 48;
- if (firstNumDigit < secondNumDigit)
- {
- firstNum[index - 1]--;
- firstNumDigit += 10;
- }
- int diff = firstNumDigit - secondNumDigit;
- result = (char)(diff + 48) + result;
- }
- if(znak)cout<<'-';int n=result.size();
- int i=0; while(result[i]=='0'&& i<n-1 )i++;
- for(; i<n; i++)cout<<result[i];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement