- 什么是高精度算法:
所谓的高精度算法指的是一般来说longlong无法解决的问题,一般的高精度算法分别为高精度加法, 高精度减法, 高精度乘法, 高精度触除法
- 高精度加法:
一般的高精度加法是一位一位的加,不存在进位,不过这种类型有很明显的局限性,比如当我们计算”1390349405930596094509405+4457842912833947289=?”这道题时,就必须要进位,我们必须要让电脑懂我们的指令,这也就对应了代码中的两层循环:
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');
也许有人会问为什么为什么要给A,B中加a的字符值-0,这就要说到c++的一个知识点:字符串
字符串中的第i个字符的ASCII码值减去0的ASCII码值,相减后的差值就是第i个字符本来所要表示的数字
那么接下来给出例题: 高精度A+B-普及-题目-ACGO题库
#include<iostream>
#include<vector>
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() - 1 || i <= B.size() - 1; i++)
{
if (i <= A.size() - 1) t += A[i];
if (i <= B.size() - 1) t += B[i];
C.push_back(t % 10);
t /= 10;
}
//注意最高位的进位
if (t)
C.push_back(1);
return C;
}
int main()
{
string a, b;
cin >> a >> b; //a=121345
//容器存放两个长整数
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;
}
- 高精度减法:
在读小学时,我们做减法都采用竖式方法,如图 1 所示。 这样,我们可以写出两个整数相减的算法。
我们就可以用 C++ 语言来模拟这个竖式减法的过程。我们可以考虑利用 C++ 的数组来存储对应数据,假设用数组 A 存储被减数 856 的每一位,具体来说就是 A1 存储个位 6,A2 存储十位 5,A3存储百位 8;类似数组 A 的结构,使用数组 B 存储减数 257;类似数组 A 的结构,使用数组 C 来存储对应的差 599。这样理论上来说,我们就可以计算无限大的数据。如上图 2 所示,下表表示对应的存储方式。
数组A | 数组B | 数组C | |
[0] | 6 | 7 | 9 |
[1] | 5 | 5 | 9 |
[2] | 8 | 2 | 5 |
总结:利用数组存储,突破存储的限制。每个位置存储 0 ~ 9 之间的数据。
#include <iostream>
#include <cstring>
using namespace std;
struct HugeInt {
int len;
int num[100001];
};
HugeInt a, b, w; //w为结果
char c[100001], d[100001];
bool negative; //负数标记
void Scan_HugeInt() { //读入两个大整数
cin >> c;
cin >> d;
if((strlen(c) < strlen(d)) || (strlen(c) == strlen(d) && strcmp(c, d) < 0)) { //若被减数小 交换 记为负数
negative = true;
swap(c, d);
}
a.len = strlen(c);
b.len = strlen(d);
for(int i=0; i<a.len; i++) a.num[a.len - i] = c[i] - '0';
for(int i=0; i<b.len; i++) b.num[b.len - i] = d[i] - '0';
}
void Minus() {
w.len = a.len; //a更大
for(int i=1; i<=w.len; i++) {
if(a.num[i] < b.num[i]) {
a.num[i+1] --; //num[i+1]减成负数也不影响
a.num[i] += 10; //借位
}
w.num[i] += a.num[i] - b.num[i];
}
while(w.num[w.len] == 0 && (w.len != 1)) w.len --; //多余的不是个位的0去掉
}
int main() {
Scan_HugeInt();
Minus();
if(negative == true) cout << "-"; //负数加负号
for(int i=w.len; i>=1; i--) cout << w.num[i]; //倒序存储 倒序输出
cout << endl;
return 0;
}
相关引用: