算法简要
-
高精度加法(A + B):遍历 低位 -> 高位,push求和后该位的结果
-
高精度减法 (A - B) :低位 -> 高位,大减小,在可借位之下,push每位之差
-
高精度乘法 (A × b) : 遍历 低位 -> 高位,求A每位和b的乘积,并只push每位的结果
-
高精度除法 (A ÷ b): 遍历A 高位 -> 低位 在有余数之下,push每次t(余数与该位的和)大于b的商
注意事项:
- 用string 读取高精度数
- 用vector数组逆序存入(加法、乘法为了方便进位,减法逆序方便运算,除法用不用都可以)
- 进行减法、乘法、除法要考虑前导0的情况
高精度加法代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int N = 100010;
vector <int> A, B;
vector <int> add(vector <int> a, vector <int> b)
{
vector <int> c;
if (a.size() < b.size()) return add(b, a);
int t = 0;//进位
for (int i = 0; i < a.size(); i ++)
{
if (i < b.size()) t += b[i];
c.push_back((a[i] + t) % 10);
t = (a[i] + t) / 10;
}
if (t > 0) c.push_back(t);
// for (int i = c.size() - 1; i >= 0; i --) if(c[i] == 0) c.pop_back();
return c;
}
string a, b;
int main()
{
cin >> a >> b;
for (int i = 0; i < a.size(); i ++) A.push_back(a[i] - '0');
for (int i = 0; i < b.size(); i ++) B.push_back(b[i] - '0');
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
auto C = add(A, B);
for (int i = C.size() - 1; i >= 0; i --) cout << C[i];
return 0;
}
高精度减法代码:
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
//判断两个数大小
bool cmp(vector <int> A, vector <int> B)
{
if (A.size() != B.size()) return A.size() > B.size();
for (int i = A.size() - 1; i >= 0; i --)
if (A[i] != B[i]) return A[i] > B[i];
return true;
}
vector <int> diff(vector <int> A, vector <int> B)
{
vector <int> C;
int t = 0;
for(int i = 0; i < A.size(); i ++)
{
t = A[i] - t;
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t >= 0) t = 0;
else t = 1;
}
for (int i = C.size() - 1; i >= 0; i --) printf("%d", C[i]);
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a, b;
cin >> a >> b;
vector <int> c, d;
for(int i = a.size() - 1; i >= 0; i --) c.push_back(a[i] - '0');
for(int i = b.size() - 1; i >= 0; i --) d.push_back(b[i] - '0');
if (cmp(c, d))
{
auto C = diff(c, d);
// for (int i = C.size() - 1; i >= 0; i --) printf("%d", C[i]);
}
else
{
auto C = diff(d, c);
cout << "-";
for (int i = C.size() - 1; i >= 0; i --) printf("%d", C[i]);
}
return 0;
}
高精度乘法代码:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector <int> mul(vector <int> A, int b)
{
vector <int> C;
int t = 0, cnt = 0;
for (int i = 0; i < A.size(); i ++)
{
t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
if (t) C.push_back(t);
//去前导0
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
vector <int> A;
int b;
string 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];
return 0;
}
高精度除法代码:
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> A;
int t;
vector <int> div(vector <int> A, int b)
{
vector <int> C;
for (int i = A.size() - 1; i >= 0; i --)
{
t = t * 10 + A[i];
C.push_back(t / b);
t %= b;
}
reverse(C.begin(), C.end());
// for (int i = 0; i < C.size(); i ++) cout << C[i];
while(C.size() > 1 && C.back() == 0) C.pop_back();
// cout << endl;
// for (int i = 0; i < C.size(); i ++) cout << C[i];
return C;
}
int main()
{
string a;
int b;
cin >> a >> b;
for (int i = a.size() - 1; i >= 0; i --) A.push_back(a[i] - '0');
auto C = div(A, b);
for (int i = C.size() - 1; i >= 0; i --) cout << C[i];
cout << endl << t;
return 0;
}