AcWing 794. 高精度除法
原题链接
简单
作者:
Value
,
2020-04-26 10:13:16
,
所有人可见
,
阅读 667
方法一
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
string a;
int b;
cin >> a >> b;
int remand = 0;
string res = "";
int tmp;
for(int i = 0; i < a.size(); i ++ ){
tmp = remand * 10 + (a[i] - '0');
res += tmp / b + '0';
remand = tmp % b;
}
int flag = 1;
for(int i = 0; i < res.size(); i ++ ){
if(flag){
if(res[i] != '0'){
flag = 0;
cout << res[i];
}
}else cout << res[i];
}
if(flag == 1) cout << "0" << endl << a << endl;
else cout << endl << remand << endl;
return 0;
}
方法二
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> div(vector<int> &A, int b, int &r){
vector<int> C;
for(int i = 0; i < A.size(); 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 = 0; i < a.size(); i ++ ) A.push_back(a[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 << r << endl;
return 0;
}