题目描述
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;
}