当前位置:网站首页>[5. high precision subtraction]
[5. high precision subtraction]
2022-06-22 02:41:00 【Little silly bird_ coding】
High precision subtraction
Ideas :
- Large integer storage ( Use arrays to store ), It is still stored in the array in reverse order
- Subtract decimals from large numbers , This is more convenient
- Consider borrowing , borrow 1 Add 10
step :
- take A and B Stored in the array in reverse order .
- hypothesis A and B Two numbers , First judgement A - B Value . Always subtract decimals from large numbers 【 for example A >= B, The result is A - B, If it is A <= B, Then the result is -(B - A)】
- Judge A0 - B0 - t >= 0 . At this point, there is no need to borrow A0 - B0 - t, If A0 - B0 - t < 0, You need to borrow A0 - B0 + 10 - t(
there t Is to judge whether to borrow , If you borrow, you will subtract the borrowed digits , If not, subtract 0)- Remove leading 0 . The so-called leading zero , There are data like this 01234, This 0 It's actually unnecessary .
- take C Number in , Print out in reverse order , Because it is stored upside down .
give an example
39246 - 1965 = 37281
Code
#include <iostream> using namespace std; #include <vector> bool cmp(vector<int> &A, vector<int>&B) { if (A.size() != B.size()) return A.size() > B.size(); // When the length of two numbers is not equal for (int i = A.size() - 1; i >= 0; i --) // Each bit of equal length is compared in turn { if (A[i] != B[i]) return A[i] > B[i]; } return true; // The two numbers are exactly equal } vector<int> sub(vector<int> &A, vector<int> &B) { vector<int> C; for (int t = 0, i = 0; i < A.size(); i ++) // The front has made A Always the largest ,t For borrow { t = A[i] - t; if (i < B.size()) t -= B[i]; //i Has more than B It's the number of digits , At this point, there is no need to reduce C.push_back((t + 10) % 10); // Combined the two situations ,t >= 0 , The amount deposited at this time is t, If t < 0, In this case, borrowing is required for storage if (t < 0) t = 1; //t < 0 Equivalent to borrowing , At this point, you need to subtract from the number borrowed 1 else t = 0; } while (C.size() > 1 && C.back() == 0) C.pop_back(); // Remove the lead 0, If it turns out to be 01234, Remove the beginning at this time 0 return C; } int main() { string a, b; vector<int> A, B; cin >> a >> b; //a = '123456' for (int i = a.size() - 1; i >= 0; i --) A.push_back(a[i] - '0'); //A =[6,5,4,3,2,1] -'0' Is to change a string into an integer for (int i = b.size() - 1; i >= 0; i --) B.push_back(b[i] - '0'); if (cmp(A, B)) { auto C = sub(A, B); for (int i = C.size() - 1; i >= 0; i --) printf("%d", C[i]); } else { auto C = sub(B, A); printf ("-"); for (int i = C.size() - 1; i >= 0; i --) printf("%d", C[i]); } return 0; }
边栏推荐
- Annual special analysis of China Mobile Banking in 2022
- MySQL recursively finds the tree structure. This method is very practical!
- Must the database primary key be self incremented? What scenarios do not suggest self augmentation?
- Creating and extending XFS file system based on LVM
- EMC輻射發射整改-原理案例分析
- 【一起上水硕系列】Day Two
- Chapter 24 image and video processing based on Simulink -- matlab in-depth learning and practical collation
- GraphAcademy 课程讲解:《Neo4j 图数据科学简介》
- PMP reference related agile knowledge
- C# 判断应用是否启动并展示
猜你喜欢

How to select the appropriate version of neo4j (version 2022)

Wechat applet film and television comment exchange platform system graduation design (3) background function

Li Kou today's question 1108 IP address invalidation

【4. 高精度加法】

带你区分几种并行

MySQL recursively finds the tree structure. This method is very practical!

LabVIEW开发发电厂合规性测试系统

从数据库的分类说起,一文了解图数据库

Chapter 25 digital watermarking technology based on Wavelet Transform

GraphAcademy 课程讲解:《Neo4j 图数据科学简介》
随机推荐
With the acceleration of industry wide digital transformation, what kind of storage will be more popular?
What is a neural network
How to obtain the comment information of tables and columns in gbase8a database?
Graphconnect 2022 at a glance
基于xposed框架hook使用
MySQL 递归查找树形结构,这个方法太实用了!
Annual special analysis of China Mobile Banking in 2022
How to select the appropriate version of neo4j (version 2022)
Flash back when GoLand starts
GetEmptyBlcoksPre Info
How to apply PMP project management knowledge?
2022钎焊考试模拟100题及答案
Qt程序怎么实现选中ListWidget中的某一行为默认选中
Live broadcast on June 22 | zhanzhihui, South China Institute of Technology: evolutionary computing for expensive optimization
PMP pre exam guide on June 25, you need to do these well
【1. 快速排序】
Development of power plant compliance test system with LabVIEW
Kubernetes code generator use
EMC輻射發射整改-原理案例分析
【7. 高精度除法】

