博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2562 Primary Arithmetic(我的水题之路——模拟加法进位)
阅读量:4069 次
发布时间:2019-05-25

本文共 2302 字,大约阅读时间需要 7 分钟。

Primary Arithmetic
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8450   Accepted: 3166

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 456555 555123 5940 0

Sample Output

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

Source

又是一道英文题。给两个不超过十位的正整数,问它们相加的过程中,一共有几次进位。
高精度模拟,做过高精度加法的同学都不会觉得太难吧。
注意点:
1)当仅有一次进位的时候,输出"operation",其他时候输出"operations".
2)进位时候,当两个数字长度不相同时,考虑多出部分的进位,如:“99999 + 1”。
代码(1AC):
#include 
#include
#include
char num1[20];char num2[20];int main(void){ int add, tmp; int carry; int i, j; int len1, len2; while (scanf("%s %s", num1, num2), strcmp(num1, "0") != 0 || strcmp(num2, "0") != 0){ getchar(); len1 = strlen(num1); len2 = strlen(num2); for (carry = add = 0, i = len1 - 1, j = len2 - 1 ; i >= 0 && j >= 0; i--, j--){ if ((num1[i] - '0') + (num2[j] - '0') + add >= 10){ add = 1; carry ++; } else{ add = 0; } } for (; i >= 0; i--){ if (num1[i] - '0' + add >= 10){ carry++; add = 1; } else { add = 0; } } for (; j >= 0; j--){ if (num2[j] - '0' + add >= 10){ carry++; add = 1; } else { add = 0; } } if (carry == 0){ printf("No carry operation.\n"); } else if (carry == 1){ printf("1 carry operation.\n"); } else{ printf("%d carry operations.\n", carry); } } return 0;}

转载地址:http://aooji.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
Android/Linux 内存监视
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
剑指offer算法题分析与整理(三)
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
WAV文件解析
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>
QT打开项目提示no valid settings file could be found
查看>>
android 代码实现圆角
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
Visual Studio 2010:C++0x新特性
查看>>
所谓的进步和提升,就是完成认知升级
查看>>