题目描述
模板题:高精度除法
C++代码
#include <bits/stdc++.h>
#include <vector>
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()); //由于除法从高位开始算,所以要reverse
while(C.size() > 1 && C.back() == 0) //可能存在商前面几位全是0情况
C.pop_back();
return C;
}
int main(){
string a;
int b;
vector<int> A, C;
cin >> a >> b;
for(int i = a.size() - 1; i >= 0; i--)
A.push_back(a[i] - '0');
int r;
C = div(A, b, r);
for(int i = C.size() - 1; i >= 0; i--)
cout << C[i];
cout << endl;
cout << r << endl;
}