取兩數餘數,相加看是否>=10,如果前面有進位carry,記得要多加1
注意題目其實並不是兩個正整數,而是兩個非負整數(包含0)
以下為程式碼
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
long long a,b;
while (cin >> a >> b)
{
if(a==b && b==0) break;
int coun=0,carry=0;
while(a!=0 || b!=0)
{
int ra=a%10,rb=b%10;
a=a/10,b=b/10;
if(ra+rb+carry>=10)
{
coun++;
carry=1;
}
else carry=0;
}
if(coun==0) cout << "No carry operation.\n";
else if (coun==1) cout << "1 carry operation.\n";
else cout << coun << " carry operations.\n";
}
return 0;
}