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;
}
	

 

posted on   itdef  阅读(3)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
历史上的今天:
2017-11-14 c++沉思录 学习笔记 第六章 句柄(引用计数指针雏形?)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示