原题链接:https://www.acwing.com/problem/content/796/

模拟手工除法,从高位往低位依次除。也正因为从高位往低位除,因此最后去除前导0的时候要首先将结果向量reverse一次。

#include <iostream>
#include <algorithm>

using namespace std;

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 a; int b, r = 0;
cin >> a >> b;
vector<int> A;
for(int i = a.length() - 1; i >= 0; --i) A.push_back(a[i] - '0');
vector<int> res = div(A, b, r);
for(int i = res.size() - 1; i >= 0; --i) printf("%d", res[i]);
printf("\n%d", r);
}