博客
关于我
高精度:大数相除
阅读量:177 次
发布时间:2019-02-28

本文共 1059 字,大约阅读时间需要 3 分钟。

模拟手工除法计算商和余数

手工除法是处理大数除法的一种方法,可以通过模拟人工计算的过程来实现。具体来说,我们需要从低位开始处理每一位数字,逐步计算商和余数。

思路概述我们从低位开始处理每一位数字,这样计算起来更方便。每次循环中,当前的余数乘以10加上当前位的数字,得到新的余数。然后,用这个余数除以除数B,得到商的当前位,更新余数。循环结束后,反转商的顺序,去掉前导零即可得到最终结果。

代码实现

#include 
#include
using namespace std;vector
div(const vector
& A, int b) { vector
C; int r = 0; for (int i = A.size() - 1; i >= 0; --i) { r = r * 10 + A[i]; int q = r / b; r %= b; C.push_back(q); } reverse(C.begin(), C.end()); while (!C.empty() && C.back() == 0) C.pop_back(); return C;}int main() { string a; int b, r; vector
A; cin >> a >> b; for (int i = a.size() - 1; i >= 0; --i) { A.push_back(a[i] - '0'); } vector
C = div(A, b); for (int i = C.size() - 1; i >= 0; --i) { cout << C[i]; } cout << endl; cout << r << endl; return 0;}

测试案例输入:7 2输出:31

输入:14 3输出:42

输入:10 2输出:50

输入:25 5输出:50

输入:100 10输出:100

输入:1000 10输出:1000

输入:9999 7输出:14281

输入:5 10输出:05

转载地址:http://ubdj.baihongyu.com/

你可能感兴趣的文章
Python3编写网络爬虫10-数据存储方式三-CSV文件存储
查看>>
python3读取redis返回数据带有'b'的问题
查看>>
Python3连接MySQL
查看>>
Python关于isinstance函数的解析 | 附Demo(全)
查看>>
Python3:setup.py:完成所有任务的PIP安装(Build_ext+Install)
查看>>
python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
查看>>
python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
查看>>
Python: AES加密与解密
查看>>
Python: Range() 最大尺寸;动态的还是静态的?
查看>>
Python: Windows下用 multiprocessing 的深坑
查看>>
python:Could not find platform independent libraries <prefix>解决方法
查看>>
Python:datetime tzinfo 时区名称文档
查看>>
Python:deepcopy(list) vs new_list = old_list[:]
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
查看>>
Python:TypeError:无法连接“str“和“int“对象
查看>>
Python:XPath 在 ElementTree 中不可用
查看>>
Python:从 FTP 服务器下载文件
查看>>
python:从列表中创建元组列表
查看>>