zoukankan      html  css  js  c++  java
  • 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.
    ", ans);
            else if (ans == 1) printf("%d carry operation.
    ", ans);
            else printf("%d carry operations.
    ", ans);
    
        }
        return 0;
    }
    


  • 相关阅读:
    递归二分法和另类二分法(不推荐,因为占用资源)
    内置函数汇总
    非递归二分法
    2018.10.23习题随笔
    java中的类修饰符、成员变量修饰符、方法修饰符。
    js函数中的this关键字
    HTML5 <script>元素async,defer异步加载
    那些迷糊人的回调
    js函数prototype属性学习(二)
    js函数prototype属性学习(一)
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770938.html
Copyright © 2011-2022 走看看