高精度除法
首先把余数初始化成 $0$,然后商就是余数 ${\times}10$ 加上被除数的某一位除上除数,余数就是对除数取模
代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
vector<int> div(vector<int> A, int b, int &r)
{
r = 0;
vector<int> C;
for (int i = A.size() - 1; i >= 0; i --)
{
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
}
//因为除法的商是从C[0]开始一个个push_back的,所以前导0是在开头
reverse(C.begin(), C.end());
while (C.back() == 0 && C.size() > 1)
C.pop_back();
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');
int r;
auto C = div(A, b, r);
for (int i = C.size() - 1; i >= 0; i --)
cout << C[i];
cout << endl << r;
return 0;
}
好啦!这篇题解到这里就结束啦!感谢观看!!!
$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\quad$$\mathcal{writer\enspace by \enspace acwing}$ : $\mathfrak{天元之弈}$