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.

输入

Each line of input contains two unsigned integers less than 10 digits. The last line of input contains 0 0.

输出

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.

样例输入

123 456
555 555
123 594
0 0

样例输出

No carry operation.
3 carry operations.
1 carry operation.

提示

题目大意就是给你两个数计算两数相加进位的次数,比如1+9=10及进位一次。
AC代码

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <set>
#include <queue>
#include <algorithm>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f3f
#define eps 1e-8
typedef long long LL;
using namespace std;

char a[13],b[13],c[13];

int main()
{
   #ifdef LOCAL
     freopen("E://in.txt","r",stdin);
   #endif // LOCAL
    while(~scanf("%s%s",a,b))
    {
        if(strcmp(a,"0")==0&&strcmp(b,"0")==0)
            break;
        CLR(c,'0');
        int len1=strlen(a);
        int len2=strlen(b);
        if(len2>len1)
        {
           swap(len1,len2);
           char te[13];
           strcpy(te,a);
           strcpy(a,b);
           strcpy(b,te);
        }
        int k=0;
        int ans=0,i,j;
        for(i=len2-1,j=len1-1;i>=0;--i,--j,++k)
        {
            c[k]+=a[j]+b[i]-'0'-'0';
            if(c[k]-'0'>=10)
            {
                c[k+1]++;
                ++ans;
            }
        }
        while(j>=0)
        {
            c[k]+=a[j]-'0';
            if(c[k]-'0'>=10)
            {
                c[k+1]++;
                ans++;
            }
            k++;
            j--;
        }
        if(c[k]-'0'>=10)
            ans++;
        //printf("%d\n",ans);
        if(ans==0)
            printf("No carry operation.\n");
        else
            if(ans==1)
            printf("1 carry operation.\n");
        else
            printf("%d carry operations.\n",ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值