POJ Problem 2562 Primary Arithmetic

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11358   Accepted: 4172

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.

Source

Waterloo local 2000.09.23

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <iostream>
#define MAX_N   106
#define MAX(a, b) (a > b)? a: b
#define MIN(a, b) (a < b)? a: b
using namespace std;

int main() {
    int a, b;
    int s1[MAX_N], s2[MAX_N];
    while (scanf("%d%d", &a, &b) ,a || b) {
        for (int i = 0; i < 20; i++) {
            s1[i] = s2[i] = 0;
        }
        int ans = 0;
        int cnt = 0;
        while (a) {
            s1[cnt++] = a%10;
            a /= 10;
        }
        cnt = 0;
        while (b) {
            s2[cnt++] = b%10;
            b /= 10;
        }
        for (int i = 0; i < 11; i++) {
            s1[i] += s2[i];
            if (s1[i] > 9) {
                ans++;
                s1[i + 1]++;
            }
        }
        if (!ans) printf("No carry operation.\n", ans);
        else if (ans == 1) printf("%d carry operation.\n", ans);
        else printf("%d carry operations.\n", ans);

    }
    return 0;
}


posted @ 2016-07-18 20:45  zprhhs  阅读(144)  评论(0编辑  收藏  举报
Power by awescnb