1350. Primary Arithmetic

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.

Sample Input

123 456
555 555
123 594
0 0

Output for Sample Input

No carry operation.
3 carry operations.
1 carry operation.

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    unsigned int m,n,sum,i;
    //cin>>m>>n;
    while(cin>>m>>n)
    {
        if(m==0&&n==0)
          break;
        unsigned int temp;
        if(m<n)
        {
            temp=m;
            m=n;
            n=temp;
        }
        unsigned int m1,n1;
        i=0;
        sum=0;
    while(m>0)
        {
        m1=m%10;
        n1=n%10;
        if(m1+n1+i>=10)
        {
            i=1;
            sum++;
        }
        else {
            i=0;
        }
        m=m/10;
        n=n/10;

    }
    while(n>0)
    {
        if(n%10+i>=10)
        {
            i=1;
            sum++;
        }
        else
        {
            i=0;
        }
        n=n/10;
    }
    if(sum==0)
    {
       cout<<"No carry operation."<<endl;
    }
    else if(sum==1)
    {
       cout<<sum<<" carry operation."<<endl;
    }
    else if(sum>1)
    {
       cout<<sum<<" carry operations."<<endl;
    }
        //cin>>m>>n;
    }
        return 0;
}

时间: 2024-10-12 09:27:59

1350. Primary Arithmetic的相关文章

UVA OJ 10035 - Primary Arithmetic

Primary Arithmetic 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.

POJ 2562 Primary Arithmetic

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10724   Accepted: 3980 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

POJ 2562 Primary Arithmetic(简单题)

[题意简述]:计算两数相加,有多少个进位. [分析]:很简单,不过还是要注意输出的细节.当进位为1时,输出的operation,没有s. 详见代码: // 216K 0Ms #include<iostream> using namespace std; int main() { int a,b; while(cin>>a>>b) { if(a == 0&&b == 0) break; // 在这贡献了n次WA~,傻! int ans = 0; int t

训练赛题解

突然想到好久以前做完这份题目没写题解.蛮来写写吧.很多细节已经忘记了.. 第一题 很简单的字符串比对是否b包含a.不包含就报NO,包含就YES..坑爹的第一次!!.把strlen放在了for循环里面..就超时了..超时了.. 注意:for里面的条件每次也会重新计算. A - All in All Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice P

计划,,留

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 一.<算法竞赛入门经典> 刘汝佳 (UVaOJ 351道题) 以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html "AOAPC I"

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

UVaOJ 112道题目-算数与代数

1.110501/10035 Primary Arithmetic (小学生算术) 注意输出格式 #include<stdio.h> #include<string.h> #include<math.h> #include<ctype.h> #include<algorithm> using namespace std; typedef long long lld; lld a,b; int main() { while(scanf("

(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html “AOAPC I”是刘汝佳(大

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************