Problem Statement and Algorithm Description

High-Precision Subtraction Study Notes

C++ Algorithm Principles3.3 KBUploaded on2026/11/28 12:48

High-Precision Subtraction Study Notes

Title Requirements

Title:High-precision subtraction: compute a − b. Both a and b are non-negative integers with no more than 240 digits.

Two non-negative integers, one per line.

Output:An integer representing the result of subtracting one integer from another.

Sample Input:

codeCopy
33333333333333333333333333333333333333333
22222222222222222222222222222222222222222

Sample output:

codeCopy
11111111111111111111111111111111111111111

1. Core Philosophy

High-precision subtraction is essentiallySimulate vertical calculationThe number is too large for the basic data type, so usestringRead, StoreArraySubtract digit by digit and handle.borrow。

II. Algorithm Steps

  1. Read and Compare Sizes

    • Read two large numbers as strings.
    • Compare sizes first and use a unified approachSubtract the smaller number from the larger oneEnsure the result is non-negative. If the minuend is smaller than the subtrahend, swap the two numbers and apply a negative sign.
    • Comparison rule: Compare by length first; if lengths are equal, compare characters position by position (lexicographical order).
  2. Reverse Storage

    • Insert the string into the array starting from the last character, i.e.,Store the ones digit at index 0Facilitates calculation and borrowing from lower to higher positions.
  3. Digit-by-digit subtraction with borrowing

    • Iterate from index 0 to the most significant bit:
    • If the current digit is insufficient for subtraction (a[i] < b[i]), borrow 1 from the higher place to serve as 10:a[i+1]--; a[i] += 10;
    • Record the difference at the current position:c[i] = a[i] - b[i]。
  4. Remove leading zeros and output

    • Output the minus sign first (if any).
    • Start from the most significant non-zero digit and output in reverse order down to the ones place.
    • When all results are 0, keep the starting point at position 0 and output one.0。

III. Common Mistakes Summary

Common Pitfalls Description
Forgot to compare the larger number minus the smaller one Failing to swap results in negative bit values and corrupted output.
Missing due to carry-over When borrowing is needed, the higher digit must subtract 1; otherwise, directly adding 10 to the current digit will cause an error.
Leading zero not removed If100 - 99 = 001, output starting from the highest non-zero digit
Result is 0 pInitial value set to 0 for guaranteed5 - 5Output0Instead of empty

IV. Reference Code (C++)

C++Copy
#include<import>
using namespace std;

int a[250], b[250], c[250]; // a[] is the minuend, b[] is the subtrahend, c[] is the difference; index 0 represents the ones place.

int main(){
    string str1, str2;
    cin >> str1 >> str2;
    int n1 = str1.length();
    int n2 = str2.length();
    character f = '+'; // '+' indicates non-negative, '-' indicates negative

    // 1. Compare sizes: if str1 < str2, swap to ensure larger minus smaller, and set negative sign.
    if(n1 < n2 || (n1 == n2 && str1 < str2)){
        swap(str1, str2);
        swap(n1, n2);
        f = '-';
    }

    // 2. Store in reverse order: units digit at index 0 for easy borrowing from lower positions
    for(int i = 0; i < n1; i++){
        a[i] = str1[n1 - 1 - i] - '0';
    }
    for(int i = 0; i < n2; i++){
        b[i] = str2[n2 - 1 - i] - '0';
    }

    // 3. Subtract digit by digit, handling borrows
    for(int i = 0; i < n1; i++){
        if(a[i] < b[i]){   Insufficient to subtract; borrow 1 as 10 from the higher digit.
            a[i + 1] -= 1;
            a[i] += 10;
        }
        c[i] = a[i] - b[i];
    }

    // 4. Output negative sign
    if(f == '-'){
        cout << '-';
    }

    // 5. Find the highest non-zero bit, remove leading zeros (initial value of p is 0 to prevent a result of all 0)
    int p = 0;
    for(int i = n1 - 1; i >= 0; i--){
        if(c[i] != 0){
            p = i;
            break;
        }
    }

    // 6. Output from the highest non-zero digit down to the units place
    for(int i = p; i >= 0; i--){
        cout << c[i];
    }
    return 0;
}

豫ICP备2026040576号-1