当前位置:网站首页>[6. high precision multiplication]
[6. high precision multiplication]
2022-06-22 02:41:00 【Little silly bird_ coding】
High precision multiplication
Ideas :
- Multiply high precision integers by low precision integers , take
Smaller numbers are treated as a whole, Every bit of a high-precision integer , Multiply by low precision integers- The next bit is multiplied by a small integer , And add the carry .
- Remove the lead 0, Because the result may be 0123456
Specific steps
- take
High precision integers AandLow precision integer b In reverse orderArrange in an array- Every bit of a high-precision integer , Multiply the whole by a low precision integer .
- Consider carrying ,
(A * b + t) %10As a result ,(A * b + t) / 10For carry .- Remove the lead 0, Because the result may be 0123456
- take C Median number , Reverse order printout .
give an example :
Code
#include <iostream> #include <vector> using namespace std; vector<int> mul(vector<int> &A, int b) { vector<int> C; int t = 0; // At the very beginning t0 = 0; for (int i = 0; i < A.size() || t; i ++) { if(i < A.size()) t += A[i] * b; C.push_back(t % 10); t /= 10; } while (C.size() > 1 && C.back() == 0) C.pop_back(); // Remove the lead 0; return C; } int main() { string a; int b; cin >> a >> b; vector<int>A; for (int i = a.size() - 1; i >= 0; i --) A.push_back(a[i] - '0'); auto C = mul(A,b); for (int i = C.size() - 1; i >= 0; i --)printf("%d", C[i]); }The core algorithm can be changed to :
vector<int> mul(vector<int> &A, int b) { vector<int> C; int t = 0; // At the very beginning t0 = 0; for (int i = 0; i < A.size() ; i ++) { t += A[i] * b; C.push_back(t % 10); t /= 10; } if (t != 0) C.push_back(t); // here t There are many situations , May be 0( No carry ), The possible carry is 1, It is also possible to carry more than 1 // There are only two cases of addition , Or carry , Carry only 1, Or not carry while (C.size() > 1 && C.back() == 0) C.pop_back(); // Remove the lead 0; return C; }
边栏推荐
- Minecraft 1.18.2 生化8 模组 1.3版本 物品3D化+更加复杂村庄
- Programming of pytorch interface
- All the knowledge you want to know about the PMP Exam is here
- 国产品牌OPPO官方最新出品!这份PPT报告!真刷新我对它认知了
- Relative references must start with either “/“, “./“, or “../“.
- A short video costs hundreds of thousands of yuan, and the virtual digital man makes a circle with "strength"
- 【8、一维前缀和】
- Review of mathematical knowledge: triple integral
- Unicode decodeerror appears: 'ASCII' codec can't decode byte 0xe9 in position 0: ordinal not in range solution
- Rational Rose installation tutorial
猜你喜欢

【6. 高精度乘法】

Chapter 25 digital watermarking technology based on Wavelet Transform

Rational Rose installation tutorial

小孩子学什么编程?

All the knowledge you want to know about the PMP Exam is here

Penetration testing - logic vulnerability topic

智翔金泰冲刺科创板:年营收3919万亏损超3亿 拟募资40亿

Using neo4j sandbox to learn neo4j graph data science GDS

Word document to markdown document?

Development of power plant compliance test system with LabVIEW
随机推荐
小孩子学什么编程?
Word document to markdown document?
EMC radiation emission rectification - principle case analysis
最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA
并查集dsu
Pytorch visualization
What is a neural network
Review of mathematical knowledge: triple integral
【9. 子矩阵和】
mocklog_ Simulation log
带你区分几种并行
目标检测之——labelImg标注工具使用方法
Fabric. JS iText set italics manually
Automated tools - monitoring file changes
Ioerror: no translation files found for default language zh cn Solutions for
On Monday, I asked the meaning of the | -leaf attribute?
The latest official product of domestic brand oppo! This ppt report! It really refreshes my understanding of it
Development of power plant compliance test system with LabVIEW
【6. 高精度乘法】
rt_thread的消息队列
