杭电1002
#include <iostream> #include <string> #include <string.h> #include <stdlib.h> using namespace std; int main() { int a[1002]; int b[1002]; int n; char aa[1002]; char bb[1002]; cin>>n; int line = 1; while (line <= n) { cin>>aa>>bb; memset(a, 0, sizeof(a)); //不要硬编码,采用sizeof,因为是int是4字节,之前自己给自己埋了个坑,写成1002。。。 memset(b, 0, sizeof(b)); int strLenA = strlen(aa); for (int i = 0; i < strLenA; i++) //数组逆转存入 { a[i] = aa[strLenA - 1 - i] - '0'; } int strLenB = strlen(bb); for (int j = 0; j < strLenB; j++) { b[j] = bb[strLenB - 1 - j] - '0'; } int maxLen = strLenA > strLenB ? strLenA : strLenB; for (int k = 0; k < maxLen; k++) //先处理高位 { a[k+1] += (a[k] + b[k]) / 10; a[k] = (a[k]+ b[k]) % 10; } cout<<"Case "<<line<<":"<<endl; cout<<aa<<" + "<<bb<<" = "; if (a[maxLen] > 0) //判断最高位是否有进位 cout<<a[maxLen]; for (int kk = maxLen - 1; kk >= 0; kk--) cout<<a[kk]; cout<<endl; if (line < n) //注意最后一个案例不要多输出空行 cout<<endl; line++; } return 0; }