题目链接
高精度模板题
#include#include #include #include using namespace std;const int maxn = 1010;int an[maxn], bn[maxn];string add(string a, string b){ memset(an, 0, sizeof an); memset(bn, 0, sizeof bn); for (int i = 0; i < a.length(); i++) an[i] = a[a.length() - i - 1] - '0'; for (int i = 0; i < b.length(); i++) bn[i] = b[b.length() - i - 1] - '0'; int len = max(a.length(), b.length()); for (int i = 0; i < len; i++){ an[i] += bn[i], an[i + 1] += an[i] / 10, an[i] %= 10; } if (an[len]) len++; string res = ""; for (int i = 0; i < len; i++) res += an[len - 1 - i] + '0'; return res;}int main(){ int t,k=1; cin >> t; string a, b; while (t--){ cin >> a >> b; cout << "Case " << k++ << ":" << endl; cout << a << " + " << b << " = "; cout << add(a, b) << endl; if (t != 0) cout << endl; } return 0;}