//大意为每个测试用例给两个整数,计算这两个整数相加的过程中产生的总进位数。 //多个测试用例,最后一行的0,0不用算 /* *感觉用字符串做更方便些 */ #include "stdio.h" #include "string" #include "algorithm" using namespace std; int main(){ char num1[11],num2[12]; while(scanf("%s %s",num1,num2)!=EOF){ string str1 = num1,str2 = num2; if (str1 == "0" && str2 == "0") break; reverse(str1.begin(),str1.end());//低字节存储地位,这样的话个位在左,不逆转的话个位在右, //从右向左做,思路都一样,没有什么区别 reverse(str2.begin(),str2.end()); int sum = 0,carry = 0;//记录进位次数的,记录当前是否进位 int length = max(str1.size(),str2.size()); while (str1.size() < length) str1 += "0"; while (str2.size() < length) str2 += "0"; for (int i = 0; i < length; ++i) { if(str1[i]-'0'+str2[i]-'0'+carry >= 10){ carry = 1; ++sum; } else carry = 0; } if (sum == 0) printf("NO carry operation.\n"); else if(sum == 1) printf("1 carry operation.\n"); else printf("%d carry operations.\n",sum); } }