当前位置:网站首页>[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; }
边栏推荐
- How to select the appropriate version of neo4j (version 2022)
- Annual special analysis of China Mobile Banking in 2022
- June25,2022 PMP Exam clearance manual-5
- Word document to markdown document?
- The "cloud" end of the 6th world intelligence conference will be held soon
- Creating and extending XFS file system based on LVM
- Create RT_ Thread thread
- What is a neural network
- Starting from the classification of database, I understand the graph database
- Rely on the robustness of trusted AI to effectively identify deep forgery and help banks fight identity fraud
猜你喜欢

【7. 高精度除法】

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

EMC整改小技巧

Graphacademy course explanation: introduction to neo4j graph data science

Unity3d post process volume profile

What is a neural network

微软 IE 浏览器于 6 月 15 日被永久关闭

【5. 高精度减法】

The neo4j skill tree was officially released to help you easily master the neo4j map database

What programming does a child learn?
随机推荐
PMP pre exam guide on June 25, you need to do these well
Review of mathematical knowledge: triple integral
Zap grammar sugar
Architecture and practice of vivo container cluster monitoring system
FPGA-Xilinx 7系列FPGA DDR3硬件设计规则
OpenJudge NOI 1.13 46:八进制到十进制
Linxu modify the permissions of the folder so that everyone can access 777
Create RT_ Thread thread
DAST black box vulnerability scanner part 4: scanning performance
No sleep at noon, sleepy in the afternoon
rt_ Message queue of thread
Select for i/0 multiplexing
Pytorch visualization
Using OKR for HR digital transformation
理想L9正式发布:8月底前开始交付 零售价45.98万元
基于xposed框架hook使用
Latest release: neo4j figure data science GDS 2.0 and aurads GA
Cmake common command category notes
Informer有什么
Chapter 21 design of pavement crack detection and identification system -- matlab deep learning practice
