AcWing 791 高精度加法 https://www.acwing.com/problem/content/description/793/
#include <iostream> #include <cstdio> #include <algorithm> #include <string> using namespace std; const int N = 1e5 + 5; int a[N]; int b[N]; //结果在a数组中 void add(int a[], int b[]) { int tmp = 0; int len = max(a[0], b[0]); int i; for(i = 1; i <= len; ++ i) { int res = a[i] + b[i] + tmp; tmp = res / 10; a[i] = res % 10; } if(tmp > 0) { a[i] = tmp; ++ a[0]; } } int main() { string A, B; cin >> A >> B; a[0] = A.length(); b[0] = B.length(); for(int i = 1; i <= a[0]; ++ i) a[i] = A[a[0] - i] - ‘0‘; for(int i = 1; i <= b[0]; ++ i) b[i] = B[b[0] - i] - ‘0‘; add(a, b); for(int i = a[0]; i > 0; -- i) printf("%d", a[i]); return 0; }
原文地址:https://www.cnblogs.com/Chaosliang/p/12188684.html
时间: 2024-11-02 04:03:52