AcWing
794. 高精度除法
实质上是,使用数组模拟除法运算的过程。注意消除结果数组中的前导0
.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<int> A;
int b;
string a;
vector<int> div(vector<int> &A, int b, int &r){
vector<int> C;
int sA = (int)A.size();
r = 0;
for(int i = sA - 1; i >= 0; i --){
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
}
reverse(C.begin(), C.end());
while((int)C.size() >= 2 && !C.back()) C.pop_back();
return C;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> a >> b;
int sa = (int)a.size();
for(int i = sa - 1; i >= 0; i --) A.push_back(a[i] - '0');
int r;
vector<int> C = div(A, b, r);
int sC = (int)C.size();
for(int i = sC - 1; i >= 0; i --) cout << C[i];
cout << '\n';
cout << r;
return 0;
}