1.加法
string a1, b1;
int a[5010], b[5010], c[5010];
signed main() {cin >> a1 >> b1;int len1 = a1.size();int len2 = b1.size();for (int i = 1; i <= len1; i++) {a[i] = a1[len1 - i] - '0';}for (int i = 1; i <= len2; i++) {b[i] = b1[len2 - i] - '0';}for (int i = 1; i <= max(len1, len2); i++) {c[i] += a[i] + b[i];c[i + 1] = c[i] / 10;c[i] %= 10;}if (c[max(len1, len2) + 1] != 0) cout << c[max(len1, len2) + 1];for (int i = max(len1, len2); i >= 1; i--) {cout << c[i];}return 0;
}
2.减法
#include<set>
#include<list>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include <stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<stack>
#include <utility>
#include<map>
#include <vector>#define inf 0x3f3f3f3f
#define int long long
const int N =1e6 + 10;
//#include <bits/stdc++.h>
typedef long long ll;
#include<iostream>
using namespace std;//long long MAX(long long a, long long b) { return a < b ? b : a; }string a1, b1;
int a[N], b[N], c[N];
bool cmp(string e1, string e2) {int len1 = e1.size();int len2 = e2.size();if (len1 > len2) return true;//e1比e2大else {for (int i = len1 - 1; i >= 0; i--) {if (e2[i] > e1[i]) return false;}}return true;//一样大
}
signed main() {cin >> a1 >> b1;int f = 0;if (!cmp(a1, b1)) {//b比a大swap(a1, b1);f = 1;}for (int i = 1; i <= a1.size(); i++) {a[i] = a1[a1.size() - i] - '0';}for (int i = 1; i <= b1.size(); i++) {b[i] = b1[b1.size() - i] - '0';}int cnt = 0;for (int i = 1; i <= max(a1.size(),b1.size()); i++) {if (a[i] < b[i]) {a[i] += 10;a[i + 1] -= 1;}c[i] = a[i] - b[i];}if (f) cout << "-";int lc = max(a1.size(), b1.size());while (c[lc] == 0 && lc > 1) lc--;//去除前导0for (int i = lc; i >= 1; i--) {cout << c[i];}return 0;
}
3.乘法
string a1, b1;
int a[N], b[N], c[N];signed main() {cin >> a1 >> b1;int la = a1.size();int lb = b1.size();for (int i = 1; i <= la; i++) {a[i] = a1[a1.size() - i] - '0';}for (int i = 1; i <= lb; i++) {b[i] = b1[b1.size() - i] - '0';}int lc = la + lb;//乘积的最长长度for (int i = 1; i <= la; i++) {for (int j = 1; j <= lb; j++) {c[i + j - 1] += a[i] * b[j];c[i + j] += c[i + j - 1] / 10;c[i + j - 1] %= 10;}}while (lc > 1 && c[lc] == 0) lc--;for (int i = lc; i >= 1; i--) cout << c[i];return 0;
}
4.除法
高精度除以低精度
模拟试除法
#include<set>
#include<list>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include <stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<stack>
#include <utility>
#include<map>
#include <vector>#define inf 0x3f3f3f3f
#define int long long
const int N =1e6 + 10;
//#include <bits/stdc++.h>
typedef long long ll;
#include<iostream>
using namespace std;//long long MAX(long long a, long long b) { return a < b ? b : a; }string a1;
int a[N], b, c[N];signed main() {cin >> a1 >> b;int la = a1.size();for (int i = 1; i <= la; i++) {a[i] = a1[i - 1] - '0';}int cur = 0;//余数for (int i = 1; i <= la; i++) {c[i] = (cur * 10 + a[i]) / b;cur = (cur * 10 + a[i]) % b;}int lc = 1;while (c[lc] == 0 && lc < la) lc++;//最终的结果位数至少是1位数,所以lc不能大于lafor (int i = lc; i <= la; i++) cout << c[i];return 0;
}
高精度除以高精度
#include <cstdio>
#include <iostream>
#include <cstring>using namespace std;int a[50005], b[50005], c[50005], d;void init(int a[]) {char s[50005];cin >> s;a[0] = strlen(s); // 字符串存储,表示位数 for (int i=1; i<=a[0]; i++) {a[i] = s[a[0]-i] - 48; // 正序储存}
}void print(int a[]) { if (a[0] == 0) {cout << 0 << endl;return; // 位数为0,输出0}for (int i=a[0]; i>=1; i--) {cout << a[i]; // 输出函数}cout << endl;return;
} int compare(int a[], int b[]) { if (a[0] > b[0]) {return 1; // 被减数大于减数} if (a[0] < b[0]) {return -1; // 被减数小于减数}for (int i=a[0]; i>=1; i--) { if (a[i] > b[i]) {return 1;} if (a[i] < b[i]) {return -1;} // 位数相同,找到第一位不同的进行比较} return 0;
}void numcpy(int p[], int q[], int det) {for (int i=1; i<=p[0]; i++) {q[i+det-1] = p[i]; //复制p数组到q数组从det开始的地方}q[0] = p[0] + det - 1;
}void jian(int a[], int b[]) { int flag = compare(a, b); if (flag == 0) { a[0] = 0;return;}if (flag == 1) { for (int i=1; i<=a[0]; i++) {if (a[i] < b[i]) { a[i+1]--; a[i] += 10;}a[i] -= b[i];}while (a[0]>0 && a[a[0]]==0) {a[0]--; } return; }
} // 高精减法void chugao(int a[], int b[], int c[]) {int tmp[50005];c[0] = a[0] - b[0] + 1;for (int i=c[0]; i>0; i--) {memset(tmp, 0, sizeof(tmp)); numcpy(b, tmp, i);// 清零while (compare(a, tmp) >= 0) {c[i]++;jian(a, tmp); // 用减法模拟 } }while (c[0] > 0 && c[c[0]] == 0) {c[0]--;}return;
}int main() {memset(a, 0, sizeof(a));memset(b, 0, sizeof(b));memset(c, 0, sizeof(c));init(a);init(b);chugao(a,b,c);print(c); return 0;
}