天天看点

POJ 2562 Primary Arithmetic 进位统计

原题 http://poj.org/problem?id=2562

题目:

Primary Arithmetic

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 11008 Accepted: 4048

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.

思路:

求两个数相加进位了多少次。

此处我们可以效仿高精度的做法,进位的同时ans++。

注意输出格式,进1位和多位的区别,有个s。

代码:

#include <iostream>
#include"string.h"
#include"cstdio"
#include"stdlib.h"
#include"algorithm"
using namespace std;
typedef long long int lint;


int main()
{
    const int N=;
    lint a,b;
    while(cin>>a>>b)
    {
        if(a==&&b==)  break;
        lint s1[N];
        lint s2[N];
        memset(s1,,sizeof(s1));
        memset(s2,,sizeof(s2));
        int l1=;
        while(a>)
        {
            s1[l1]=a%;
            l1++;
            a=a/;
        }
        int l2=;
        while(b>)
        {
            s2[l2]=b%;
            l2++;
            b=b/;
        }
        int ans=;
        for(int i=; i<max(l1,l2)+; i++)
        {
            s1[i]=s1[i]+s2[i];
            if(s1[i]>=)
            {
                s1[i]=s1[i]%;
                s1[i+]++;
                ans++;
            }
        }
        if(ans==)  printf("No carry operation.\n");
        else if(ans==) printf("1 carry operation.\n");
        else
        printf("%d carry operations.\n",ans);

    }
    return ;
}