题目链接:HDU 1002 A + B Problem II
题目大意:
计算(A+B)。
题解:
高精度模板题,注意对数组的初始化。
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct BigNum {
int num[1010], len;
} a, b, c;
void add(BigNum &a, BigNum &b, BigNum &c) {
c.len = max(a.len, b.len);
for (int i = 0; i < c.len; ++i) {
c.num[i] = a.num[i] + b.num[i];
}
for (int i = 0; i < c.len; ++i) {
if (c.num[i] > 9) {
c.num[i + 1]++;
c.num[i] -= 10;
}
}
if (c.num[c.len]) c.len++;
}
void print(BigNum x) {
for (int i = x.len - 1; i >= 0; --i) {
cout << x.num[i];
}
}
int main() {
int T;
cin >> T;
for (int p = 1; p <= T; ++p) {
string str;
memset(a.num, 0, sizeof(a.num));
memset(b.num, 0, sizeof(b.num));
memset(c.num, 0, sizeof(c.num));
cin >> str;
int t = str.length();
a.len = t;
for (int i = 0; i < t; ++i) {
a.num[i] = str[t - i - 1] - '0';
}
cin >> str;
t = str.length();
b.len = t;
for (int i = 0; i < t; ++i) {
b.num[i] = str[t - i - 1] - '0';
}
add(a, b, c);
cout << "Case " << p << ":" << endl;
print(a);
cout << " + ";
print(b);
cout << " = ";
print(c);
if (p == T) cout << endl;
else cout << endl << endl;
}
return 0;
}