Problem Statement and Algorithm Description
High-Precision Addition Algorithm Notes
High-Precision Addition (Big Integer Addition) Algorithm Notes
Applicable problem: Calculate a + b, where a and b are non-negative integers not exceeding 240 digits (standard
long longCan't save
1. Core Philosophy
When a number is too large to fit within the built-in integer type, it can only bestringSave it. The essence of high-precision addition isSimulate elementary school vertical column addition:
- Align numbers by place value (ones with ones, tens with tens).
- Add digits from right to left
- Carry over when reaching 10
II. Three-Step Problem-Solving Method
1. Reverse storage (complete "alignment")
StringReverseStore in integer array: array index0Place1Store in ten positions...
This way, indices with the same value correspond to the same digit place. Simply add them directly without handling unequal lengths.
- Save:
a[i] = str1[n1 - 1 - i] - '0'(Character reduction'0'Convert to number - When arrays are of different lengths, the extra positions in the shorter array are naturally
0, just right for zero-padding
2. Add digit by digit (simulate vertical addition)
Loop count: use the longer lengthlen = max(n1, n2)
- Current result:
sum % 10 - New Carry:
sum / 10 - The next step should include this carry-over:
sum = a[i] + b[i] + jinwei
3. Handle most significant bit carry
If, after the loop ends,jinwei > 0, must write it downc[len]and letlen++, otherwise99 + 1will output00rather than100。
Finally, reconstruct the string in reverse order from the most significant digit to the least significant digit.+ '0'Convert numbers back to strings.
III. Reference Code
#include <import>
#include
using namespace std;
int a[250], b[250], c[250]; // Slightly larger than the maximum digit count to prevent carry overflow
int main() {
string str1, str2, str3;
cin >> str1 >> str2;
int n1 = str1.size();
int n2 = str2.size();
int len = max(n1, n2);
// 1. Store in reverse order to complete "alignment"
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';
// 2. Simulate vertical addition
int jinwei = 0, sum = 0;
for (int i = 0; i < len; i++) {
sum = a[i] + b[i] + jinwei; // Don't forget to add the carry
c[i] = sum % 10; // Leave only the units place for the current digit
jinwei = sum / 10; // Calculate new carry
}
// 3. Handle carry from the most significant bit
if (jinwei > 0) {
c[len] = jinwei;
len++;
}
// 4. Reconstruct the result in reverse order and output
for (int i = len - 1; i >= 0; i--) str3 += (c[i] + '0');
cout << str3;
return 0;
}IV. Common Pitfalls
| Common Pitfalls | Best practice |
|---|---|
| Convert between characters and numbers | On Save- '0', output+ '0'Forgot about getting ASCII codes |
| Forgot to add the carry | summust bea[i] + b[i] + jinweithree items |
| Carry out of most significant bit lost | Check after loopjinwei > 0, fill in andlen++ |
| Array index out of bounds | Problem 240 bits, array size must be at least 250. |
| Leading Zeros / Output Order | The result array is in little-endian order; output must start fromlen - 1Reverse |
5. One-Sentence Memory
Store in reverse, add digit by digit, carry over when reaching ten, and don't forget the final carry from the most significant digit.
VI. Expansion Directions
The same framework can be easily adapted to implement high-precision subtraction and multiplication by an integer: it still uses "reverse storage + digit-by-digit processing + separate carry/borrow tracking," but withsum % 10 / sum / 10Replace with corresponding borrow handling or multiply-accumulate.
