Primary Arithmetic UVA - 10035
// Primary Arithmetic UVA - 10035.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
https://vjudge.net/problem/UVA-10035
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.
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> va, vb;
void fill(long long a, vector<int>& u) {
u.clear();
if (a == 0) {
u.push_back(0); return;
}
while (a) {
u.push_back(a % 10);
a /= 10;
}
return;
}
void solve() {
int len = min(va.size(), vb.size());
int ans = 0;
int carry = 0;
int idx;
for (idx = 0; idx < len; idx++) {
carry = (va[idx] + vb[idx]+ carry) /10;
if (carry > 0) ans++;
}
while (idx < va.size() ) {
carry = (va[idx] + carry) / 10;
if (carry > 0) ans++;
idx++;
}
while (idx < vb.size() ) {
carry = (vb[idx] + carry) / 10;
if (carry > 0) ans++;
idx++;
}
//if (carry != 0) ans++;
if (ans == 0) {
cout << "No carry operation." << endl;
}
else if (ans == 1) {
cout << "1 carry operation." << endl;
}
else {
cout << ans << " carry operations." << endl;
}
}
int main()
{
long long a, b;
while (cin >> a >> b) {
if (a == 0 && b == 0) break;
fill(a, va);
fill(b, vb);
solve();
}
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力

