高精度四则运算
1、高精度加法
#include <string>
#include <iostream>
#include <vector>
using namespace std;
vector<int> add(vector<int> &A, vector<int>&B) //高精度加法
{
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 % 10); //取个位,进上去值被加到t里面去了
t /= 10;
}
if (t) C.push_back(t); //如果还有最后一个进上去的位数没有被加上,通过这条语句来加上
return C;
}
int main ()
{
vector<int>A , B;
string a, b;
cin >> 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--) //倒着输出
{
cout << C[i];
}
return 0;
}
2、高精度减法
//高精度减法
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector <int> sub(vector<int>&A, vector<int>&B)
{
if (A.size() < B.size()) return sub(B, A);
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); //按照借位的方法存入(t负数的话,借一加十,正数的话,取余留个位)
//借位处理
if(t < 0) t = 1; //通过下一次循环,还回去那个1 (t = A[i] - t)
else t = 0;
}
while (C.size() > 1 && C.back() == 0) C.pop_back(); //去除002这种情况,只输出2
return C;
}
int main ()
{
string a, b;
cin >> a >> b;
vector<int> A, B;
for (int i = a.size() - 1; i >= 0; i--)
{
A.push_back(a[i] - '0');
}
for (int j = b.size() - 1; j >= 0; j--)
{
B.push_back(b[j] - '0');
}
auto C = sub(A, B);
for (int i = C.size() - 1; i >= 0; i--)
{
cout << C[i];
}
return 0;
}
3、高精度乘以低精度
//高精度乘以低精度
#include <iostream>
#include <string>
#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++) // ||t的原因是最后如果t存有数据,将其全部给掉C,或的原因是防止 0 * b而结束循环;
{
if (i < A.size()) t += A[i] * b; //每个数据都乘以b,
C.push_back(t % 10);
t /= 10; //有进位的情况
}
while (C.size() > 1 && C.back() == 0) C.pop_back(); //解决002这种情况而输出2
return C;
}
int main ()
{
string a;
int b = 0;
cin >> a >> b;
vector<int> A;
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;
}
4、高精度除于低精度
//高精度除以低精度
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> div (vector<int>&A, int b, int &r)
{
vector<int>C;
int t = 0;
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(); //解决002这种情况而输出2
return C;
}
int main ()
{
string a;
int b = 0;
int r = 0;
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--)
{
cout << C[i];
}
cout << endl << r; //余数
return 0;
}
5、高精度乘以高精度
数据每位数据在1000010
//高精乘以高精度
#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
#include <set>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
using namespace std;
int A[10010], B[10010], c[10010];
char a[10010], b[10010];
int main()
{
int lena, lenb;
cin >> a >> b;
lena = strlen(a);
lenb = strlen(b);
for (int i = 1; i <= lena; i++)
{
A[i] = a[lena - i] - '0';
}
for (int i = 1; i <= lenb; i++)
{
B[i] = b[lenb - i] - '0';
}
for (int i = 1; i <= lena; i++)//计算乘法的时候先不考虑进位,先让i + j - 1; 而i就是我们说的错位相加。
{
for (int j = 1; j <= lenb; j++)
{
c[i + j - 1] += A[i] * B[j];
}
}
for (int i = 1; i < lena + lenb; i++)//无论数字如何大,数位也不会大于两个乘数的和
{
if (c[i] > 9) //大九需要进位
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
int len = lena + lenb;
while (c[len] == 0 && len > 1) len--;//去除前导0
for (int i = len ; i >= 1; i--)
{
cout << c[i];
}
return 0;
}