因为想要加减乘除形式统一,所以除法结束之后还要reverse一下, 倒置数列
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> div(vector<int> &A, int b, int &r)
{
vector<int> C;
int t = 0;
for(int i = A.size() - 1; i >= 0; i -- )
{
t = t * 10 + A[i];
C.push_back(t / b);
t = t % b;
}
r = t;
reverse(C.begin(), C.end());
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a;
int b, r;
cin >> a >> b;
vector<int> A;
for(int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '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;
}