本文共 914 字,大约阅读时间需要 3 分钟。
#include <stdio.h>
#include <string> #include <iostream>using namespace std;
string str1, str2, ans;
int main()
{ int n; scanf("%d", &n); getchar(); int count = 1; while(n--) { cin >> str1 >> str2; string x1 = str1, x2 = str2; int len1 = str1.length(), len2 = str2.length(); string tmp; //对齐 if(len1 < len2) { for(int i = 0; i < len2-len1; i++) tmp = tmp + '0'; str1 = tmp + str1; } if(len1 > len2) { for(int i = 0; i < len1-len2; i++) tmp = tmp + '0'; str2 = tmp + str2; } bool flag = false; int i, id = 0; for(i = str1.length()-1; i >= 0; i--) { int tmp = str1[i] + str2[i] - '0' - '0' + flag; if(tmp >= 10) { flag = true; char s = '0' + tmp % 10; ans = s + ans; } else { flag = false; char s = '0' + tmp; ans = s + ans; } } if(flag) //比如像9+1这种情况 ans = '1' + ans; cout << "Case " << count << ":" << endl; cout << x1 << " + " << x2 << " = " << ans << endl; ans.clear(); count++; if(n) cout << endl; } return 0; }转载地址:http://yfrn.baihongyu.com/