参考:【算法模板】高精度模板(带图详解)SuhyOvO的博客-CSDN博客
1.正整数加法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| string add(string x, string y) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); int la = x.length(); int lb = y.length(); int lc = max(la, lb); for (int i = 0; i < la; i++) a[i] = x[la - i - 1] - '0'; for (int i = 0; i < lb; i++) b[i] = y[lb - 1 - i] - '0'; for (int i = 0; i < lc; i++) { c[i] += a[i] + b[i]; c[i + 1] += c[i]/10; c[i] %= 10; } lc++; while (lc > 1 && c[lc-1] == 0) lc--; string res = ""; for (int i = lc- 1; i >= 0; i--){ res.push_back(char(c[i] + '0')); } return res; }
|
2.正整数减法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| string sub(string sa, string sb){ memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); la = sa.length(); lb = ab.length(); lc = max(la, lb); string sub = ""; if(la < lb || (la == lb && (x[la-1]-'0' < y[lb-1]-'0'))){ swap(la, lb); swap(sa, sb); sub = "-"; } for (int i = 0; i < la; i++) a[i] = x[la - i - 1] - '0'; for (int i = 0; i < lb; i++) b[i] = y[lb - 1 - i] - '0'; for (int i = 0; i < lc; i++) { if (a[i] < b[i]) { a[i + 1] -= 1; a[i] += 10; } c[i] = a[i] - b[i]; } while ((c[lc - 1] == 0) && (lc > 1)) lc--; string res = ""; for (int i = lc-1; i >= 0; i--){ res.push_back(c[i] + '0'); } return sub + res; }
|
3.高精乘低精
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| string mul(string x, int y) { la = x.length(); string ans = "";
if (x[0] == '-' && y > 0) { x = x.substr(1, la); la--; ans.push_back('-'); } else if (x[0] != '-' && y < 0) { y = abs(y); ans.push_back('-'); } else if (x[0] == '-' && y < 0) { x = x.substr(1, la); la--; y = abs(y); }
for (int i = 0; i < la; i++) { a[i] = x[la - i - 1] - '0'; }
for (int i = 0; i < la; i++) { a[i] = a[i] * y + tmp; tmp = a[i] / 10; a[i] %= 10; } if (tmp != 0) { a[la++] = tmp; while (a[la - 1] > 0) { a[la] = a[la - 1] / 10; a[la - 1] %= 10; la++; } } while ((a[la - 1] == 0) && (la > 1)) la--;
for (int i = la - 1; i >= 0; i--) { ans.push_back(a[i] + '0'); } return ans; }
|
4.高精乘高精
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| string High_Mul(string x, string y) { string ans = ""; la = x.length(); lb = y.length(); if (x[0] == '-' && y[0] != '-') { x = x.substr(1,la--); ans.push_back('-'); } else if (x[0] != '-' && y[0] == '-') { y = y.substr(1,lb--); ans.push_back('-'); } else if (x[0] == '-' && y[0] == '-') { x = x.substr(1,la--); y = y.substr(1,lb--); }
for (int i = 0; i < la; i++) a[i] = x[la - i - 1] - '0'; for (int i = 0; i < lb; i++) b[i] = y[lb - 1 - i] - '0'; lc = la + lb ; for (int i = 0; i < la; i++) { for (int j = 0; j < lb; j++) { c[i + j] += a[i] * b[j]; c[i + j + 1] += c[i + j] / 10; c[i + j] %= 10; } } while ((c[lc - 1] == 0) && (lc > 1)) lc--; for (int i = lc- 1; i >= 0; i--){ ans.push_back(c[i] + '0'); } return ans; }
|
5.高精除以低精