题意:计算两个整数相加时需要多少次进位。程序可以连续处理多组数据,直到读到两个0结束。(输入的整数都不超过9个数字)
输入:
123 456
555 555
123 594
输出:
0
3
1
分析:输入的数字不超过9个数字,可以用int表示。每次把a和b分别模10得到个位数。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b&&a!=0&&b!=0){
int c=0; //表示进位
int ans=0;
for(int i=9;i>=0;i--){
c=(a%10+b%10+c)/10;
if(c>0) ans++;
a/=10;
b/=10;
}
cout<<ans<<endl;
}
return 0;
}