Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- bool is_A_smaller_than_B(string A, string B) {
- if(A.size() < B.size()) {
- return true;
- }
- if(A.size() > B.size()) {
- return false;
- }
- for(int i = 0; i < (int) A.size(); i++) {
- if(A[i] < B[i]) {
- return true;
- }
- if(A[i] > B[i]) {
- return false;
- }
- }
- return false;
- }
- string subtract_big_integer(string A, string B) {
- if(is_A_smaller_than_B(A, B)) {
- swap(A, B);
- }
- int sz_a = (int)A.size() - 1;
- int sz_b = (int)B.size() - 1;
- int carry = 0;
- string diff = "";
- while(sz_a >= 0 and sz_b >= 0) {
- int digit_a = (A[sz_a] - '0');
- int digit_b = (B[sz_b] - '0');
- int razlika = digit_a - digit_b - carry;
- if(razlika < 0) {
- razlika += 10;
- carry = 1;
- }
- else {
- carry = 0;
- }
- diff += (razlika + '0');
- sz_a--;
- sz_b--;
- }
- while(sz_a >= 0) {
- int digit_a = (A[sz_a] - '0');
- int razlika = digit_a - carry;
- if(razlika < 0) {
- carry = 1;
- razlika += 10;
- }
- else {
- carry = 0;
- }
- diff += (razlika + '0');
- }
- reverse(diff.begin(), diff.end());
- return diff;
- }
- int main()
- {
- cout << subtract_big_integer("98421", "19999") << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement