1 #include <iostream> 2 #include <memory.h> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 char a[1000], b[1000], s[1001]; 9 int t; 10 cin >> t; 11 int ct = 1; 12 while(t--) 13 { 14 cin >> a >> b; 15 int lena = strlen(a) - 1; 16 int lenb = strlen(b) - 1; 17 int j, p = 0; 18 for(j = 0; lena >= 0 || lenb >= 0; ++j, --lena, --lenb) 19 { 20 if(lena >= 0 && lenb >= 0) s[j] = a[lena] + b[lenb] - ‘0‘ + p; 21 if(lena >= 0 && lenb < 0) s[j] = a[lena] + p; 22 if(lena < 0 && lenb >= 0) s[j] = b[lenb] + p; 23 p = 0; 24 if(s[j] - ‘0‘ > 9) 25 { 26 s[j] -= 10; 27 p = 1; 28 } 29 } 30 cout << "Case " << ct++ << ":\n"; 31 cout << a << " + " << b << " = "; 32 if(p) cout << 1; 33 for(--j; j >= 0; --j) 34 cout << s[j]; 35 cout << endl; 36 if(0 != t) cout << endl; 37 } 38 return 0; 39 }
时间: 2024-10-02 09:20:11