Primary Arithmetic

Children are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the
“carry” operation - in which a 1 is carried from one digit position to be added to the next - to be a
significant challenge. Your job is to count the number of carry operations for each of a set of addition
problems so that educators may assess their difficulty.
Input
Each line of input contains two unsigned integers less than 10 digits. The last line of input contains ‘0
0’.
Output
For each line of input except the last you should compute and print the number of carry operations
that would result from adding the two numbers, in the format shown below.
Sample Input
123 456
555 555
123 594
0 0
Sample Output
No carry operation.
3 carry operations.
1 carry operation.
代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    //freopen("F:\\1234.txt","r",stdin);
    //freopen("F:\\12345.txt","w",stdout);
    string m,n;
    while(cin>>m>>n&&(m!="0"||n!="0"))
    {
        int cnt=0;
        int flag=0;
        for(int i=m.size()-1,j=n.size()-1;i>=0||j>=0;i--,j--)
        {
            int s=0;
            if(i>=0)
                s+=m[i]-'0';
            if(j>=0)
                s+=n[j]-'0';
            if(s+flag>=10)
            {
                cnt++;
                flag=1;
            }
            else
                flag=0;
        }
        if(cnt==0)
            cout<<"No carry operation."<<endl;
        else if(cnt==1)
            cout<<cnt<<" carry operation."<<endl;
        else
            cout<<cnt<<" carry operations."<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43797508/article/details/88693731