高精度加法
https://www.acwing.com/problem/content/793/
使用vector的高精度
#include<bits/stdc++.h>
using namespace std;
vector<int> add(vector<int> A, vector<int> B) {
//将长度更长的作为第一个参数
if(A.size() < B.size()) return add(B, A);
vector<int> ans;
int t = 0;
for(int i = 0; i < A.size(); i++) {
t += A[i];
if(i < B.size()) t += B[i];
ans.push_back(t % 10);
t /= 10;
}
//处理一下最后的进位
while(t) {
ans.push_back(t % 10);
t /= 10;
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string a, b;
vector<int> 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');
vector<int> ans = add(A, B);
for(int i = ans.size() - 1; i >= 0; i--) cout << ans[i];
return 0;
}
使用数组模型高精度(更快一点)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 15;
int ans_size; // 答案的长度
int a[N], b[N], ans[N];
void add(int a[], int b[], int len) {
int t = 0;
for(int i = 0; i < len; i++) {
t += a[i] + b[i];
ans[ans_size++] = (t % 10);
t /= 10;
}
//处理最后一位进位
while(t) {
ans[ans_size++] = (t % 10);
t /= 10;
}
return ;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s1, s2;
cin >> s1 >> s2;
//逆序存储两个数
for(int i = s1.size() - 1; i >= 0; i--) {
a[s1.size() - 1 - i] = s1[i] - '0';
}
for(int i = s2.size() - 1; i >= 0; i--) {
b[s2.size() - 1 - i] = s2[i] - '0';
}
int len = max(s1.size(), s2.size());
add(a, b, len);
//逆序输出答案
for(int i = ans_size - 1; i >= 0; i--) cout << ans[i];
return 0;
}
高精度减法
https://www.acwing.com/problem/content/description/794/
使用vector的高精度
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
bool cmp(vector<int> A, vector<int> B) {
if(A.size() != B.size()) return A.size() > B.size();
else {
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> ans;
int t = 0;
for(int i = 0; i < A.size(); i++) {
t = A[i] - t;
if(i < B.size()) t -= B[i];
ans.push_back((t + 10) % 10);
if(t < 0) t = 1;
else t = 0;
}
//去除前导0
while(ans.size() > 1 && ans.back() == 0) ans.pop_back();
return ans;
}
int main() {
string a, b;
vector<int> 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');
vector<int> ans;
if(cmp(A, B)) ans = sub(A, B);
else {
cout << "-";
ans = sub(B, A);
}
for(int i = ans.size() - 1; i >= 0; i--) cout << ans[i];
return 0;
}
数组模拟高精度(快一点)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 15;
int ans_size;
int a[N], b[N], ans[N];
bool cmp(string s1, string s2) {
if(s1.size() != s2.size()) return s1.size() > s2.size();
else {
for(int i = 0; i < s1.size(); i++) {
if(s1[i] != s2[i]) return s1[i] > s2[i];
}
}
return true;
}
void sub(int a[], int b[], int len) {
int t = 0;
for(int i = 0; i < len; i++) {
t = a[i] - b[i] - t;
ans[ans_size++] = ((t + 10) % 10);
if(t < 0) t = 1;
else t = 0;
}
//这里因为a > b,所以在循环结束后必然t > 0
//处理前导0
while(ans_size > 1 && ans[ans_size - 1] == 0) ans_size--;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s1, s2;
cin >> s1 >> s2;
//逆序存储两个数
for(int i = s1.size() - 1; i >= 0; i--) {
a[s1.size() - 1 - i] = s1[i] - '0';
}
for(int i = s2.size() - 1; i >= 0; i--) {
b[s2.size() - 1 - i] = s2[i] - '0';
}
int len = max(s1.size(), s2.size());
if(!cmp(s1, s2)) {
cout << "-" ;
sub(b, a, len);
}else sub(a, b, len);
//逆序输出答案
for(int i = ans_size - 1; i >= 0; i--) cout << ans[i];
return 0;
}
高精度乘法
https://www.acwing.com/problem/content/795/
使用vector
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
vector<int> mul(vector<int> A, int b) {
vector<int> ans;
int t = 0;
for(int i = 0; i < A.size(); i++) {
t += A[i] * b;
ans.push_back(t % 10);
t /= 10;
}
//加上最后一位进位
if(t) ans.push_back(t);
//去除前导0
while(ans.size() > 1 && ans.back() == 0) ans.pop_back();
return ans;
}
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');
vector<int> ans = mul(A, b);
for(int i = ans.size() - 1; i >= 0; i--) cout << ans[i];
return 0;
}
数组模拟
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 15;
int ans_size;
int a[N], ans[N];
void mul(int a[], int b, int len) {
int t = 0;
for(int i = 0; i < len; i++) {
t += a[i] * b;
ans[ans_size++] = (t % 10);
t /= 10;
}
//处理进位位
while(t) {
ans[ans_size++] = (t % 10);
t /= 10;
}
//去除前导0
while(ans_size > 1 && ans[ans_size - 1] == 0) ans_size--;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s1;
int b;
cin >> s1 >> b;
for(int i = s1.size() - 1; i >= 0; i--) {
a[s1.size() - 1 - i] = s1[i] - '0';
}
int len = s1.size();
mul(a, b, len);
for(int i = ans_size - 1; i >= 0; i--) cout << ans[i];
return 0;
}
高精度除法
https://www.acwing.com/problem/content/796/
使用vector
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
vector<int> div(vector<int> A, int b, int &r) {
vector<int> ans;
for(int i = 0; i < A.size(); i++) {
r = r * 10 + A[i];
ans.push_back(r / b);
r %= b;
}
return ans;
}
int main() {
string a;
int b;
vector<int> A;
cin >> a >> b;
//不用颠倒次序,从高位算起
for(int i = 0; i < a.size(); i++) A.push_back(a[i] - '0');
int r = 0, k = 0;
vector<int> ans = div(A, b, r);
//去除前导0
while(k < ans.size() - 1 && ans[k] == 0) k++;
for( ; k < ans.size(); k++) cout << ans[k];
cout << endl << r << endl;
return 0;
}
数组模拟
ps: 如果b最大可以取到 1 * 10^9,那么r在计算过程中就会爆int。就需要用long long 来存储
下面洛谷这题就是:
https://www.luogu.com.cn/problem/P1480
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 15;
typedef long long LL;
int ans_size, b;
LL r;
int a[N], ans[N];
void div(int a[], int b, int len, LL &r) {
for(int i = 0; i < len; i++) {
r = r * 10 + a[i];
ans[ans_size++] = (r / b);
r %= b;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s1;
cin >> s1 >> b;
//除法可以顺序存储,不需要逆序
//(你想逆序存储也可以~)
for(int i = 0; i < s1.size(); i++) {
a[i] = s1[i] - '0';
}
int len = s1.size();
div(a, b, len, r);
//去除前导0
int i = 0;
while(i < ans_size - 1 && ans[i] == 0) i++;
for( ; i < ans_size; i++) cout << ans[i];
// cout << endl << r << endl;
return 0;
}