10035 - 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.

在小学时我们都做过加法的运算,就是把2个整数靠右对齐然后,由右至左一位一位相加。如果相加的结果大于等于10就有进位(carry)的情况出现。你的任务就是要判断2个整数相加时产生了几次进位的情况。这将帮助小学老师分析加法题目的难度。

Input

每一列测试资料有2个正整数,长度均小于10位。最后一列有2个0代表输入结束。

Output

每列测试资料输出该2数相加时产生多少次进位,请参考Sample Output。注意进位超过1次时operation有加s

Sample Input

123 456
555 555
123 594
0 0

Sample Output

No carry operation.
3 carry operations.
1 carry operation.
解题思路:每个数单独判断是否进位,c在第二组数据的初始化,同时输出时t>1时的“s”要记得加
#include<stdio.h>
int main()
{long a=0,b=0,c,n1=0,n2=0,t=0,i;
while(scanf("%ld %ld",&n1,&n2)!=EOF){
                if(n1==0&&n2==0) break;
                for(c=t=0;;){
                                            a=n1%10;
                                            b=n2%10;
                                            n1=n1/10;
                                            n2=n2/10;
                                            if((a+b+c)>=10){
                                                          t++;
                                                          c=(a+b+c)/10;
                                                          }
                                            else c=0; 
                                            if(n1==0&&n2==0)break;
                                            }
                if(t==0)printf("No carry operation.\n");
                else if(t==1)printf("1 carry operation.\n");
                else printf("%ld carry operations.\n",t);
                }
return 0;
    }

posted on 2013-02-03 23:10  喂喂还债啦  阅读(1081)  评论(0编辑  收藏  举报