题目链接:1372.Problem C:Primary Arithmetic
Description
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.
分析:统计有两个数相加时,一共进了多少次位。。。模拟
如果你wa了,请注意输出格式,3 carry operations.和 1 carry operation.的区别就是operation的单复数,zex。。。。
#include<stdio.h>
#include<iostream>
#include<map>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<string>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
//typedef long long ll;
#define MAXN 1005
#define INF 0x3f3f3f3f//将近int类型最大数的一半,而且乘2不会爆int
#define MOD 1000000007
char a[MAXN], b[MAXN];
int s[MAXN];
int len;
int sum(char *a, char *b) //大数模拟
{
int cc=0;
memset(s, 0, sizeof(s));
int la = strlen(a);
int lb = strlen(b);
reverse(a, a + la);
reverse(b, b + lb);
for (int i = 0; i < la; ++i)
a[i] = a[i] - '0';
for (int i = 0; i < lb; ++i)
b[i] = b[i] - '0';
len = max(la, lb);
int ans, tem = 0;
for (int i = 0; i <= len; ++i)
{
ans = a[i] + b[i] + tem;
s[i] = ans % 10;
tem = ans / 10;
if(tem == 1) //进位统计
cc++;
}
return cc;
}
int main()
{
while(scanf("%s%s", a, b) && (a[0]!='0' || b[0]!='0'))
{
memset(s, 0, sizeof(s));
int ans = sum(a, b);
if(ans > 1)
printf("%d carry operations.\n", ans);
else if(ans == 1)
printf("%d carry operation.\n", ans);
else
printf("No carry operation.\n");
}
return 0;
}