AcWing 791. 高精度加法
原题链接
简单
#include<iostream>
#include <algorithm>
#include <cstring>
using namespace std;
void bigNumAdd(string A, string B, int* a, int* b, int*& c) {
int i;
for (i = 1; i <= A.size(); i++)a[A.size() - i] = A[i - 1] - '0';
for (i = 1; i <= B.size(); i++)b[B.size() - i] = B[i - 1] - '0';
for (i = 0; i < max(A.size(), B.size()) + 1; i++){
c[i] = 0;
}
for (i = 0; i < min(A.size(), B.size()); i++) {
c[i] += a[i] + b[i];
if (c[i] >= 10) {
c[i] %= 10;
c[i + 1] = 1;
}
}
if (A.size() < B.size()) exchange(A, B);
while (A.size() > i) {
c[i] += a[i];
i++;
}
for (int n = 0; n <= i; n++) {
if (n == 0 && c[i - n] == 0);
else cout << c[i - n];
}
}
int main() {
string A, B;
cin >> A >> B;
int* a = (int*)malloc(A.size() * sizeof(int));
int* b = (int*)malloc(B.size() * sizeof(int));
int* c = (int*)malloc((max(A.size(),B.size())+1) * sizeof(int));
bigNumAdd(A, B, a, b, c);
free(a);
free(b);
free(c);
return 0;
}