当前位置:网站首页>高精度减法
高精度减法
2022-08-10 22:46:00 【Ding Jiaxiong】
题目
给定两个正整数(不含前导 0),计算它们的差,计算结果可能为负数。
输入格式
共两行,每行包含一个整数。
输出格式
共一行,包含所求的差。
数据范围
1≤整数长度≤105
输入样例:
32
11
输出样例:
21
思路分析
题解
#include <iostream>
#include <vector>
using namespace std;
bool cmp(vector<int> &A, vector<int> &B)
{
if (A.size() != B.size()) return A.size() > B.size();
for (int i = A.size() - 1; i >= 0; i -- )
if (A[i] != B[i])
return A[i] > B[i];
return true;
}
vector<int> sub(vector<int> &A, vector<int> &B)
{
vector<int> C;
for (int i = 0, t = 0; i < A.size(); i ++ )
{
t = A[i] - t;
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t < 0) t = 1;
else t = 0;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a, b;
vector<int> A, B;
cin >> a >> b;
for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0');
vector<int> C;
if (cmp(A, B)) C = sub(A, B);
else C = sub(B, A), cout << '-';
for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i];
cout << endl;
return 0;
}
边栏推荐
- LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
- HGAME 2022 Final writeup
- Power system power flow calculation (Newton-Raphson method, Gauss-Seidel method, fast decoupling method) (Matlab code implementation)
- ITK 读取一个目录中的一个序列,然后改变头信息,将多张dcm图像写成一个dcm文件。
- STL-deque
- 【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
- 怎么关闭电脑广告
- 68:第六章:开发文章服务:1:内容梳理;article表介绍;创建【article】文章服务;
- What would happen if disconnecting during the process of TCP connection?
- Research on multi-element N-k fault model of power system based on AC power flow (implemented by Matlab code) [Power System Fault]
猜你喜欢
常用代码扩展点设计方式
诺诚健华通过注册:施一公家族身价15亿 高瓴浮亏5亿港元
这款可视化工具神器,更直观易用!太爱了
How to be a Righteous Hacker?What should you study?
数学建模准备知识
Power system power flow calculation (Newton-Raphson method, Gauss-Seidel method, fast decoupling method) (Matlab code implementation)
How does the Weiluntong touch screen display the current value of abnormal data while alarming?
Research on multi-element N-k fault model of power system based on AC power flow (implemented by Matlab code) [Power System Fault]
虎牙自动发弹幕换牌子
瑞幸咖啡第二季营收33亿:门店达7195家 更换CFO
随机推荐
使用方便、易于集成、可扩展的用于物流运输行业的文档管理软件
Splunk中解决数据质量问题常见日期格式化
mysql中的三大日志
LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
【秋招】【更新中ing】手撕代码系列
金山云CEO王育林离职:正值冲刺港股之际 雷军曾送金砖
BM13判断一个链表是否为回文结构
OneNote 教程,如何在 OneNote 中整理笔记本?
leetcode:355. 设计推特
实例054:位取反、位移动
LeetCode Daily 2 Questions 01: Reverse Strings (both 1200) Method: Double Pointer
STL-deque
MySQL:MySQL的集群——主从复制的原理和配置
N1BOOK writeup
【MySQL】mysql因为字符集导致left join出现Using join buffer (Block Nested Loop)
合并k个已排序的链表
Glide监听Activity生命周期源码分析
Qualcomm Platform Development Series Explanation (Application) Introduction to QCMAP Application Framework
诺诚健华通过注册:施一公家族身价15亿 高瓴浮亏5亿港元
实例049:lambda