加法
#include<bits/stdc++.h>
using namespace std;
vector <int> add(vector<int> &A,vector<int> &B)
{
vector<int> C;
int t = 0;//表示进位
for (int i = 0; i < A.size() || i < B.size(); i ++ )
{
if(i<A.size()) t += A[i];
if(i<B.size()) t += B[i];
C.push_back(t%10);
t/=10;
}
if(t!=0) C.push_back(1);
return C;
}
int main()
{
string a, b;
cin >> a >> b;//输入123456789
vector <int> A,B;
for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0');
auto c=add(A,B);
for(int i = c.size() - 1;i >= 0; i --) printf("%d",c[i]);
return 0;
}
除法
#include <iostream>
#include <cstring>
#include <algorithm>
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 = r % B;
}
reverse(C.begin(), C.end());
while(C.size() > 1 && C.back() == 0)//去除前导0
C.pop_back();
return C;
}
int main()
{
string a;
vector<int> A;
int B;
cin >> a >> B;
for (int i = 0; i < a.size(); i ++ )
A.push_back(a[i] - '0');
int r = 0;//余数
auto C = div(A, B, r);
reverse(C.begin(), C.end());
for (int i = 0; i < C.size(); i ++ )
cout << C[i];
cout << endl << r;
return 0;
}