// 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;
}
标签:10,vb,idx,va,++,Primary,carry,10035,UVA
From: https://www.cnblogs.com/itdef/p/18546581