高精度除法
重点
1. r表示余数, 模拟手动除法,每次除完r都会乘10并加上下一个数作为下一位对除数
2. 除完后对c是逆序存的
3. 需要去除前导0
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 100100;
vector<int> div(vector<int> &a, int b, int &r) {
vector<int> c;
r = 0;
for (int i = a.size() - 1; i >= 0; i--) {
r = r * 10 + a[i];
c.push_back(r / b);
r %= b;
}
reverse(c.begin(), c.end());
while (c.size() > 1 && c.back() == 0) c.pop_back();
return c;
}
int main() {
string s;
int b;
vector<int> a;
cin >> s >> b;
for (int i = s.size() - 1; i >= 0; i--) {
a.push_back(s[i] - '0');
}
int r = 0;
vector<int> c = div(a, b, r);
for (int i = c.size() - 1; i >= 0; i--) cout << c[i];
cout << endl;
cout << r << endl;
return 0;
}