标签:hduoj sum
2 1 2 112233445566778899 998877665544332211
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
#include<iostream> #include <string> #include<algorithm> using std::endl; using std::cin; using std::cout; using std::string; int main() { #ifdef LOCAL freopen("input.txt" , "r" , stdin); freopen("output.txt" , "w" , stdout); #endif int T; cin >> T; string a , b , sum; for(int cases = 1; cases<=T; ++cases) { sum.clear(); cin >> a >> b; cout << "Case " << cases << ":" << endl; cout << a <<" + "<<b << " = "; int c = 0 ; reverse(a.begin() , a.end()); reverse(b.begin() , b.end()); int alength = a.length(); int blength = b.length(); int length = alength; //使两个字符串的长度相等,补齐零 if(alength < blength) { for(int i=alength; i<blength; ++i) { a += '0'; } length = blength; } if(blength < alength) { for(int i=blength; i<alength; ++i) { b +='0'; } length = alength; } //进行加法计算,c为进位 for(int i=0; i<length; ++i) { int temp = (a[i] - '0') + (b[i] - '0') +c; c = temp/10; sum += (temp%10 + '0'); } //最高位的进位如果不为0的话则进位 if(c!=0) { sum += (c +'0'); } //结果转置 reverse(sum.begin() , sum.end()); cout << sum << endl; //控制最后的那组数据不再输出空行 if(cases != T) { cout << endl; } } }
HDU1002--A + B Problem II,布布扣,bubuko.com
标签:hduoj sum
原文地址:http://blog.csdn.net/computer_liuyun/article/details/29221715