HDOJ大数加法 请大家帮我修改一下!
程序代码:
#include"iostream" #include"string" using namespace std; int main() { int t; cin >> t; while (t--) { int flag = 1; char a[1010], b[1010], c[1011]; memset(a, '0', sizeof(a)); memset(b, '0', sizeof(b)); memset(c, '0', sizeof(c)); cin >> a >> b; int l = (strlen(a) >= strlen(b)) ? strlen(a) : strlen(b); for (int l = strlen(a), i = 0; i < l/2; i++) { char t = a[i]; a[i] = a[l - i - 1]-48; a[l - i - 1] = t-48; } for (int l = strlen(b), i = 0; i < l/2; i++) { char t = b[i]; b[i] = b[l - i - 1]-48; b[l - i - 1] = t-48; } for (int i = 0; i < l; i++) { c[i] = a[i] + b[i]; for (int i = 0; i < l; i++) if (c[i] >= 10) { c[i] = c[i] % 10; c[i + 1] =c[i]+ 1; } } cout << "Case " << flag << ":" << endl; for (int i = strlen(a) - 1; i >= 0; i--) { a[i] += 48; if (a[i] == '0') continue; cout << a[i]; } cout << " + "; for (int i = strlen(b) - 1; i >= 0; i--) { b[i] += 48; if (b[i] == '0') continue; cout << b[i]; } cout << " = "; for (int i = 1005; i >= 0; i--) { if (c[i] == '0') continue; c[i] += 48; cout << c[i]; } cout << endl; flag++; } }