当前位置:网站首页>acwing794 高精度除法
acwing794 高精度除法
2022-06-12 16:08:00 【_刘小雨】
给定两个非负整数(不含前导 0) A,B,请你计算 A/B 的商和余数。
输入格式
共两行,第一行包含整数 A,第二行包含整数 B。
输出格式
共两行,第一行输出所求的商,第二行输出所求余数。
数据范围
1≤A的长度≤100000,
1≤B≤10000,
B 一定不为 0
输入样例:
7
2
输出样例:
3
1
思路:
一步一步进行除;
余数 + 下一位,在进行除;
直到计算到最后一位。
#include <iostream>
#include <vector>
#include <algorithm> // reverse函数
using namespace std;
// A / b 余数 是 c
vector<int> div(vector<int>& A, int b, int& c)
{
vector<int> re; // 商
c = 0;
// 除法这里是从高位开始计算的, 存储时654321, 其他的是从前往后计算
for(int i = A.size() - 1; i >= 0; i --)
{
c = c * 10 + A[i]; // 前面的余数加上后面位数
re.push_back(c / b);
c %= b;
}
reverse(re.begin(), re.end());
while(re.size() > 1 && re.back() == 0) re.pop_back();
return re;
}
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'); // 654321
int r; // 余数
auto C = div(A, b, r);
for(int i=C.size() - 1; i>=0 ;i--) cout<< C[i];
cout << endl;
cout << r << endl;
return 0;
}
边栏推荐
- Great God cracked the AMD k6-2+ processor 22 years ago and opened the hidden 128KB L2 cache
- [thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions
- Scanpy (VI) analysis and visualization of spatial transcriptome data
- 联通网管协议框图
- Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]
- 读取mhd、raw图像并切片、归一化、保存
- Development practice of ag1280q48 in domestic CPLD
- 一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤
- Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]
- FPGA (III) trigger and latch
猜你喜欢

Writing code can also be classified as "manual" or "vulgar", and we should be good at finding good hands!

The common hand, the original hand and the excellent hand from the sum of Fibonacci sequence

Step by step to create a trial version of ABAP program containing custom screen

With a lamp inserted in the nostril, the IQ has risen and become popular in Silicon Valley. 30000 yuan is enough

ER diagram made by StarUML based on the last student achievement management system
![[practical case of light source] UV-LED curing innovation makes the production line more smooth](/img/6f/04f52a37782c54b1050f908f46eadf.png)
[practical case of light source] UV-LED curing innovation makes the production line more smooth

Classic case of solidity - Smart games

Fiddler packet capturing (mobile app)

Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)

Saga architecture pattern: implementation of cross service transactions under microservice architecture
随机推荐
[OWT server] customized script 3: server construction
redis String类型常见命令
Five models of software testing
Go Net Library (to be continued)
第一章 线性表
acwing 800. 数组元素的目标和
Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)
Reprise de Google net
Task fruit Juicer 0611
聊聊事件监听那些事-上
鼻孔插灯,智商上升,风靡硅谷,3万就成
C language partition bin file program
Chapter I linear table
读取mhd、raw图像并切片、归一化、保存
Difference between tinyint and int
C语言 分割bin文件程序
Huawei equipment is configured with CE dual attribution
Project training of Software College of Shandong University rendering engine system basic renderer (6)
Decision tree classification and examples
Dongmingzhu talks about batteries: the most essential thing is safety