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驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


标签:
挑战编程:程序设计竞赛训练手册
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
2017-11-14 c++沉思录 学习笔记 第六章 句柄(引用计数指针雏形?)