点击查看代码
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> div(vector<int>& A, int b, int &r)
{
vector<int> C;
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;
cin >> a >> b;
vector<int> A;
for (int i = a.size() - 1; i >= 0; i --) A.push_back(a[i] - '0');
int r = 0;
auto C = div(A, b, r);
for (int i = C.size() - 1; i >= 0; i --) printf("%d", C[i]);
cout << endl << r << endl;
return 0;
}
- 用 r 记录余数,每计算一个新的除法时,res = res * 10 + A[ i ],res * 10 类似于除法操作中把余数挪下来这个操作;
- C 记录的是从高位到低位,要进行反转,然后去除前导的 0;
- div 关于 r 的参数要传入 & r;
本文摘自 :https://www.cnblogs.com/