当前位置:网站首页>【6. 高精度乘法】
【6. 高精度乘法】
2022-06-22 02:32:00 【小呆鸟_coding】
高精度乘法
思路:
- 高精度整数乘以低精度的整数,将
较小的数看成整体,高精度整数的每一位,依次乘以低精度整数- 下一位与小的整数相乘,并且再加上进位。
- 去掉前导0,因为结果可能是0123456
具体步骤
- 将
高精度整数A和低精度整数b倒序排放在数组中- 高精度整数的每一位,依次乘以低精度整数这个整体。
- 考虑进位,
(A * b + t) %10为结果,(A * b + t) / 10为进位.- 去掉前导0,因为结果可能是0123456
- 将C中数数字,倒序打印输出。
举例:
代码
#include <iostream> #include <vector> using namespace std; vector<int> mul(vector<int> &A, int b) { vector<int> C; int t = 0; //最开始的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(); //去掉前导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]); }核心算法可以改成:
vector<int> mul(vector<int> &A, int b) { vector<int> C; int t = 0; //最开始的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); //此时t有多种情况,可能为0(不进位),可能进位为1,也可能进位大于1 //加法这里只有俩种情况,要么进位,进位只进 1,要么不进位 while (C.size() > 1 && C.back() == 0) C.pop_back(); //去掉前导0; return C; }
边栏推荐
- postgresql根据时间字段的大小来取数
- Fabric. JS iText set italics manually
- June25,2022 PMP Exam clearance manual-4
- 国产品牌OPPO官方最新出品!这份PPT报告!真刷新我对它认知了
- Wechat applet Film & TV Review Exchange Platform System Graduation Design (4) Rapport d'ouverture
- Wechat applet film and television comment exchange platform system graduation design (2) applet function
- Wechat applet film and television comment exchange platform system graduation design completion (6) opening defense ppt
- C # judge whether the application is started and displayed
- WinForm project console debugging mode
- Chrome browser cancel input box to record form input history
猜你喜欢

优秀的 Verilog/FPGA开源项目介绍(二十七)- 小型CPU

微信小程序影視評論交流平臺系統畢業設計畢設(4)開題報告

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

Annual special analysis of China Mobile Banking in 2022

Leetcode 41 - 45 dynamic planning topic

EMC輻射發射整改-原理案例分析
![[Chapter 17 corner feature detection based on Harris -- actual combat of MATLAB machine learning project]](/img/41/211f6603a6854b427321c50127c2a4.png)
[Chapter 17 corner feature detection based on Harris -- actual combat of MATLAB machine learning project]

目标检测之——labelImg标注工具使用方法

Unicode decodeerror appears: 'ASCII' codec can't decode byte 0xe9 in position 0: ordinal not in range solution

Neo4j 技能树正式发布,助你轻松掌握Neo4j图数据库
随机推荐
EMC輻射發射整改-原理案例分析
Minecraft 1.18.2 biochemical 8 module version 1.3 3D objects + more complex villages
基于xposed框架hook使用
自动化工具-监测文件的变化
What programming does a child learn?
[Chapter 20 video target detection based on inter frame difference method -- Application of MATLAB software in-depth learning]
使用 OKR 進行 HR 數字化轉型
如何选择合适的 Neo4j 版本(2022版)
When retail digitalization enters a new stage of development, we need to connect the public domain with the private domain
[proteus simulation] INT0 and INT1 interrupt count
[Chapter 17 corner feature detection based on Harris -- actual combat of MATLAB machine learning project]
Penetration testing - logic vulnerability topic
EMC rectification tips
Brief analysis of application source code of neo4j intelligent supply chain
In the era of industrial Internet, there is no real center
Review of mathematical knowledge: triple integral
Using hook based on xposed framework
MySQL 递归查找树形结构,这个方法太实用了!
How to obtain the comment information of tables and columns in gbase8a database?
微信小程序影视评论交流平台系统毕业设计毕设(6)开题答辩PPT

