高精度加法算法模板
首先,我们要知道高精度算法是C++才用的,Java中是不需要高精度算法的
高精度加法: 两个大的数相加 (数的范围是 大约为10^6)
这里用vector来做
模版题
#include<vector>
#include<iostream>
#include<cstdio>
#include<string>
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) C.push_back(t); //倒着写,最高位进位的时候方便,直接push_back就行
return C;
}
int main()
{
string str1, str2;
cin >> str1 >> str2;
vector<int> A, B;
for(int i = str1.size() - 1; i >= 0; i--) A.push_back(str1[i] - '0');
for(int i = str2.size() - 1; i >= 0; i--) B.push_back(str2[i] - '0');
auto C = add(A, B);
for(int i = C.size() - 1; i >= 0; i--)
cout << C[i];
cout <<endl;
}
高精度减法算法模板
模版题
#include<iostream>
#include