本文最后更新于 2025-05-12T16:27:36+08:00
加
注意看位数!不支持负数!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 string jia (string a, string b) { string c; int x[1005 ] = {} , y[1005 ] = {} , z[1005 ] = {}; for (int i = a.size () - 1 , j = 0 ; i >= 0 ; i-- , j++){ x[i] = a[j] - '0' ; } for (int i = b.size () - 1 , j = 0 ; i >= 0 ; i-- , j++){ y[i] = b[j] - '0' ; } int len = max (a.size () , b.size ()); for (int i = 0 ; i < len ; i++){ z[i] += x[i] + y[i]; z[i + 1 ] += z[i] / 10 ; z[i] %= 10 ; } if (z[len] > 0 )len++; for (int i = len - 1 ; i >= 0 ; i--){ c += char (z[i] + '0' ); } return c; }
减
注意看位数!不支持负数!
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 string jian (string a, string b) { string c; int x[1005 ] = {} , y[1005 ] = {} , z[1005 ] = {}; for (int i = a.size () - 1 , j = 0 ; i >= 0 ; i-- , j++){ x[i] = a[j] - '0' ; } for (int i = b.size () - 1 , j = 0 ; i >= 0 ; i-- , j++){ y[i] = b[j] - '0' ; } int len = max (a.size () , b.size ()); for (int i = 0 ; i < len ; i++){ z[i] += x[i] - y[i]; if (z[i] < 0 ){ z[i + 1 ]--; z[i] += 10 ; } } for (int i = len - 1 ; i >= 0 ; i--){ c += char (z[i] + '0' ); } int pos = 0 ; while (pos < c.size () - 1 && c[pos] == '0' )pos++; return c.substr (pos); }
乘
注意看位数!不支持负数!
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 string cheng (string x, string y) { if (x == "0" || y == "0" )return "0" ; string c; int a[1005 ] = {} , b[1005 ] = {} , z[2005 ] = {}; for (int i = x.size () - 1 , j = 1 ; i >= 0 , j <= x.size () ; i-- , j++){ a[j] = x[i] - '0' ; } for (int i = y.size () - 1 , j = 1 ; i >= 0 , j <= y.size () ; i-- , j++){ b[j] = y[i] - '0' ; } for (int i = 1 ; i <= x.size () ; i++){ for (int j = 1 ; j <= y.size () ; j++){ z[i + j - 1 ] += a[i] * b[j]; z[i + j] += z[i + j - 1 ] / 10 ; z[i + j - 1 ] %= 10 ; } } int len = x.size () + y.size (); if (z[len] == 0 )len--; for (int i = len ; i >= 1 ; i--){ c += char (z[i] + '0' ); } int pos = 0 ; while (pos < c.size () - 1 && c[pos] == '0' )pos++; return c.substr (pos); }
高精除单精
不支持余数与小数结果!不支持负数!
1 2 3 4 5 6 7 8 9 10 11 12 string chu (string a , long long b) { long long sum = 0 ; string ans; for (int i = 0 ; i < a.size () ; i++){ sum = sum * 10 + (a[i] - '0' ); ans += to_string (sum / b); sum %= b; } int pos = 0 ; while (ans[pos] == '0' && pos < ans.size ())pos++; return ans.substr (pos); }
高精除高精
注意看位数!不支持小数结果!不支持负数!
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 43 44 45 46 47 string sum;string jian (string a, string b) { string c; int x[1005 ] = {} , y[1005 ] = {} , z[1005 ] = {}; for (int i = a.size () - 1 , j = 0 ; i >= 0 ; i-- , j++){ x[i] = a[j] - '0' ; } for (int i = b.size () - 1 , j = 0 ; i >= 0 ; i-- , j++){ y[i] = b[j] - '0' ; } int len = max (a.size () , b.size ()); for (int i = 0 ; i < len ; i++){ z[i] += x[i] - y[i]; if (z[i] < 0 ){ z[i + 1 ]--; z[i] += 10 ; } } for (int i = len - 1 ; i >= 0 ; i--){ c += char (z[i] + '0' ); } int pos = 0 ; while (pos < c.size () - 1 && c[pos] == '0' )pos++; return c.substr (pos); }bool cmp (string x , string y) { if (x.size () != y.size ()) return x.size () < y.size (); return x < y; }string chu (string a , string b) { string ans; for (int i = 0 ; i < a.size () ; i++){ if (sum == "0" ) sum = "" ; sum += a[i]; int cnt = 0 ; while (!cmp (sum , b)){ sum = jian (sum , b); cnt++; } ans += to_string (cnt); } int pos = 0 ; while (ans[pos] == '0' && pos < ans.size () - 1 )pos++; return ans.substr (pos); }
其中 s u m sum s u m 是余数。
高精小数除法
除数与被除数不支持高精!不支持负数!
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 #include <bits/stdc++.h> using namespace std;int n , m , k; string ans;int main () { cin >> n >> m >> k; if (n < 0 && m > 0 || m < 0 && n > 0 ){ ans += "-" ; } n = abs (n); m = abs (m); ans += to_string (n / m); ans = ans + "." ; int y = n % m; for (int i = 1 ; i <= k ; i++){ y *= 10 ; ans += to_string (y / m); y %= m; } int pos = ans.size () - 1 ; while (ans[pos] == '0' )pos--; if (ans[pos] == '.' )pos--; ans = ans.substr (0 , pos + 1 ); cout << ans; return 0 ; }
C++高精度模板
http://zhangyimin12345.github.io/posts/cmamfvq5s000jh836ec8m0epm/