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

大坑CODE:
#include <iostream>
#include <cstdio>
#include <cstdio>
#define REP(i, s, n) for(int i = s; i <= n; i ++)
#define REP_(i, s, n) for(int i = n; i >= s; i --)
#define MAX_N 10 + 5

using namespace std;

char a[MAX_N], b[MAX_N];
int int_a[MAX_N], int_b[MAX_N], la, lb;

int main(){
    while(scanf("%s%s", a + 1, b + 1) != EOF){
        if(a[1] == ‘0‘ && b[1] == ‘0‘) break;
        memset(int_a, 0, sizeof(int_a)); memset(int_b, 0, sizeof(int_b));
        la = strlen(a + 1), lb = strlen(b + 1);
        REP(i, 1, la) int_a[la - i + 1] = a[i] - ‘0‘;
        REP(i, 1, lb) int_b[lb - i + 1] = b[i] - ‘0‘;

        int i = 1, x = 0, res = 0;
        while(i <= la || i <= lb){
            x = int_a[i] + int_b[i] + x;
            x /= 10;
            if(x != 0) res ++;
            i ++;
        }

        if(res == 0) printf("No carry operation.\n");
        else if(res == 1) printf("%d carry operation.\n", res);
        else printf("%d carry operations.\n", res);
    }
    return 0;
}
				
时间: 2024-10-08 10:15:04

POJ 2562 Primary Arithmetic的相关文章

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

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.

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 co

训练赛题解

突然想到好久以前做完这份题目没写题解.蛮来写写吧.很多细节已经忘记了.. 第一题 很简单的字符串比对是否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

HOJ 题目分类

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

计划,,留

下面给出的题目共计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”是刘汝佳(大