# 高精度运算

## 高精度加法

##### 不进行位压缩
1. 把数字当字符串读入
2. 读入后倒转并且转成数字，保存在数组中
3. 按照位数进行加法运算(**注意：**有些题目可能需要去除前导0)
//加法模板
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;
}
while(c.size()>1 && c.back()==0) c.pop_back();
return c;
}

##### 进行位压缩
1. 初始化工作与不压缩一样
2. 数组中每一个下标对应的变量保存多位数字
3. 加法运算时，再把每一个下标对应的变量中的数字按位拆开进行加法运算
//压缩9位
#include <iostream>
#include <vector>
using namespace std;
const int base = 1000000000;
if (A.size() < B.size()) return add(B, A);
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i ++ ){
t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % base);
t /= base;
}
if (t) C.push_back(t);
return C;
}
int main(){
string a, b;
vector<int> A, B;
cin >> a >> b;
for (int i = a.size() - 1, s = 0, j = 0, t = 1; i >= 0; i -- ){
s += (a[i] - '0') * t;
j ++, t *= 10;
if (j == 9 || i == 0){
A.push_back(s);
s = j = 0;
t = 1;
}
}
for (int i = b.size() - 1, s = 0, j = 0, t = 1; i >= 0; i -- ){
s += (b[i] - '0') * t;
j ++, t *= 10;
if (j == 9 || i == 0){
B.push_back(s);
s = j = 0;
t = 1;
}
}
cout << C.back();
for (int i = C.size() - 2; i >= 0; i -- ) printf("%09d", C[i]);
cout << endl;
return 0;
}


## 高精度减法

#include<iostream>
#include<vector>
#include<string>
using namespace std;
bool cmp(vector<int>&A,vector<int>&B){
if(A.size() > B.size())return true;
if(A.size() < B.size())return false;

for(int i=A.size()-1;i>=0;i--){
if(A[i]!=B[i]){
return A[i]>B[i];
}
}
return true;
}
vector<int> sub(vector<int>&A,vector<int>&B){
vector<int>c;
int t = 0,x = 0;
for(int i=0;i<A.size();i++){
x = A[i]-t;
if(i<B.size()) x = x-B[i];//很重要！！！
if(x<0){
t = 1;
x+=10;
}else{
t=0;
}
c.push_back(x);
}
while(c.size()>1 && c.back()==0) c.pop_back();
return c;
}
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;
}
while(c.size()>1 && c.back()==0) c.pop_back();
return c;
}*/

int main(){
vector<int>A,B;
string a,b;
cin>>a>>b;
int la = a.length(),lb = b.length();
for(int i=la-1;i>=0;i--)A.push_back(a[i]-'0');
for(int i=lb-1;i>=0;i--)B.push_back(b[i]-'0');
/*减法*/
vector<int>c,d;
if(cmp(A,B)==true){
c = sub(A,B);
}
else{
c = sub(B,A);
putchar('-');
}
for(int i=c.size()-1;i>=0;i--)printf("%d",c[i]);
puts("");
/*加法*/
for(int i=c.size()-1;i>=0;i--)printf("%d",d[i]);
puts("");*/
return 0;
}


## 高精度乘法（小高精）

#include <iostream>
#include <vector>
using namespace std;
vector<int> mul(vector<int> &A, int b){
vector<int> C;
int t = 0;
for (int i = 0; i < A.size() || t; i ++ ){
if (i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
return C;
}
int main(){
string a;
int B;
vector<int> A;
cin >> a >> B;
for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
auto C = mul(A, B);
for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i];
cout << endl;
return 0;
}


## 高精度除法（小高精）

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> div(vector<int> &A, int b, int &r){
vector<int> C;
r = 0;
for (int i = A.size() - 1; i >= 0; 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;
vector<int> A;
int B;
cin >> a >> B;
for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
int r;
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;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> div(vector<int> &A, int b, int &r){
vector<int> C;
r = 0;
for (int i = 0 ; i < A.size() ; i ++ ){
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
}
return C;
}
int main(){
string a;
vector<int> A;
int B;
cin >> a >> B;
for (int i = 0; i < a.length() ; i ++ ) A.push_back(a[i] - '0');
int r;
vector<int> C = div(A, B, r);
int i = 0;
while (C.size() > 1 && C[i] == 0 ) i++;
for (; i < C.size() ; i ++ ) cout << C[i];
cout << endl << r << endl;
return 0;
}


©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客