大整数除普通整数
思路和乘法差不多,主要从余数下手解决
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> Div(vector<int> &A,int B,int &r)
{
vector<int> C;
//r为余数
r = 0;
for(int i = A.size()-1;i>=0;i--)
{
r = r * 10 + A[i];
C.push_back(r/B);
r = r%B;
}
reverse(C.begin(),C.end());
while(C.size()>1 && C.back()==0) C.pop_back();
return C;
}
int main()
{
string num1;
int num2;
cin>>num1>>num2;
vector<int> A;
for(int i = num1.size() - 1;i>=0;i--) A.push_back(num1[i] - '0');
int B = num2;
int r;
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;
}