74-小学生算术
内存限制:64MB
时间限制:3000ms
特判: No
通过数:23
提交数:53
难度:1
题目描述:
很多小学生在学习加法时,发现“进位”特别容易出错。你的任务是计算两个三位数在相加时需要多少次进位。你编制的程序应当可以连续处理多组数据,直到读到两个0(这是输入结束标记)。
输入描述:
输入两个正整数m,n.(m,n,都是三位数)
输出描述:
输出m,n,相加时需要进位多少次。
样例输入:
123 456 555 555 123 594 0 0
样例输出:
0 3 1
分析:
1、以一个数来存放进位的信息,要注意每次都应该对该数进行修改
核心代码:
1 while(n) 2 { 3 int t = (n%10) + (m%10) + b; 4 b = t / 10; 5 if (t >= 10) ++ cnt; 6 n /= 10, m /= 10; 7 }
C/C++代码实现(AC):
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 #include <stack> 7 #include <map> 8 #include <queue> 9 #include <set> 10 11 using namespace std; 12 const int MAXN = 310; 13 int A[MAXN]; 14 15 int main() 16 { 17 int n, m; 18 while(scanf("%d%d", &n, &m), n || m) 19 { 20 int b = 0, cnt = 0; 21 while(n) 22 { 23 int t = (n%10) + (m%10) + b; 24 if (t >= 10) ++ cnt; 25 b = t / 10; 26 n /= 10, m /= 10; 27 } 28 printf("%d ", cnt); 29 } 30 return 0; 31 }