uva 10494(大数加减乘除取余一条龙模板代码) uva 10023 (大数
发布时间:2020-12-14 03:31:33 所属栏目:大数据 来源:网络整理
导读:借用了网上的大数代码,小白上的高精度补全了。 代码: /*1.高精度加法2.高精度减法3.高精度乘法4.高精度除以低精度5.高精度对低精度的取余必要时可以将全局的long long替换成long long.除了main函数的返回值long long用到除法和取余的时候可能需要把全局的l
借用了网上的大数代码,小白上的高精度补全了。 代码: /* 1.高精度加法 2.高精度减法 3.高精度乘法 4.高精度除以低精度 5.高精度对低精度的取余 必要时可以将全局的long long替换成long long.除了main函数的返回值long long 用到除法和取余的时候可能需要把全局的long long替换成long long */ #include <cstdio> #include <iostream> #include <cstring> #include <climits> #include <cassert> using namespace std; #define maxn 30000 struct bign { long long len,s[maxn]; bign() { memset(s,sizeof(s)); len = 1; } bign(long long num) { *this = num; } bign(const char* num) { *this = num; } bign operator = (long long num) { char s[maxn]; sprintf(s,"%d",num); *this = s; return *this; } bign operator = (const char* num) { len = strlen(num); for (long long i = 0; i < len; i++) s[i] = num[len-i-1] - '0'; return *this; } string str() const { string res = ""; for (long long i = 0; i < len; i++) res = (char)(s[i] + '0') + res; if (res == "") res = "0"; return res; } /*去除前导0*/ void clean() { while(len > 1 && !s[len-1]) len--; } /*高精度的加法*/ bign operator + (const bign& b) const { bign c; c.len = 0; for (long long i = 0,g = 0; g || i < max(len,b.len); i++) { long long x = g; if (i < len) x += s[i]; if (i < b.len) x += b.s[i]; c.s[c.len++] = x % 10; g = x / 10; } return c; } /*高精度的减法*/ bign operator - (const bign& b) { bign c; c.len = 0; for (long long i = 0,g = 0; i < len; i++) { long long x = s[i] - g; if (i < b.len) x -= b.s[i]; if (x >= 0) g = 0; else { g = 1; x += 10; } c.s[c.len++] = x; } c.clean(); return c; } /*高精度的乘法*/ bign operator * (const bign& b) { bign c; c.len = len + b.len; for (long long i = 0; i < len; i++) for (long long j = 0; j < b.len; j++) c.s[i+j] += s[i] * b.s[j]; for (long long i = 0; i < c.len-1; i++) { c.s[i+1] += c.s[i] / 10; c.s[i] %= 10; } c.clean(); return c; } /*高精度除以低精度*/ /*用到除法和取余的时候可能需要把全局的int替换成long long*/ bign operator / (long long b) const { assert(b > 0); bign c;c.len = len; for (long long i = len-1,g = 0; i >= 0; --i) { long long x = 10*g+s[i]; //这里可能会超过int 故用long long c.s[i] = x/b; //这里可能会超过int g = x-c.s[i]*b; //这里可能会超过int } c.clean(); return c; } /*高精度对低精度取余*/ /*用到除法和取余的时候可能需要把全局的int替换成long long*/ bign operator % (long long b) { assert(b > 0); bign d = b; bign c = *this-*this/b*d; return c; } bool operator < (const bign& b) const { if (len != b.len) return len < b.len; for (long long i = len-1; i >= 0; i--) if (s[i] != b.s[i]) return s[i] < b.s[i]; return false; } bool operator > (const bign& b) const { return b < *this; } bool operator <= (const bign& b) { return !(b > *this); } bool operator >= (const bign& b) { return !(b < *this); } bool operator == (const bign& b) { return !(b < *this) && !(*this < b); } bool operator != (const bign& b) { return (b < *this) || (*this < b); } bign operator += (const bign& b) { *this = *this + b; return *this; } }; istream& operator >> (istream &in,bign& x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream &out,const bign& x) { out << x.str(); return out; } int main() { bign a; char b; long long c; while (cin >> a >> b >> c) { if (b == '/') cout << a/c << endl; else cout << a%c << endl; } } 大数开方: #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <climits> #include <cassert> using namespace std; const int maxn = 1000 + 10; struct bign { int s[maxn]; bign () { memset(s,sizeof(s)); } bign (int number) { *this = number; } bign (const char* number) { *this = number; } bign operator = (char *num) { s[0] = strlen(num); for (int i = 1; i <= s[0]; i++) s[i] = num[s[0] - i] - '0'; return *this; } bign operator = (int num) { char str[maxn]; sprintf(str,num); return *this = str; } bool operator < (const bign& b) const { if (s[0] != b.s[0]) return s[0] < b.s[0]; for (int i = s[0]; i; i--) if (s[i] != b.s[i]) return s[i] < b.s[i]; return false; } bool operator > (const bign& b) const { return b < *this; } bool operator <= (const bign& b) const { return !(b < *this); } bool operator >= (const bign& b) const { return !(*this < b); } bool operator != (const bign& b) const { return b < *this || *this < b; } bool operator == (const bign& b) const { return !(b != *this); } bign operator + (const bign& c) { int sum = 0; bign ans; ans.s[0] = max(s[0],c.s[0]); for (int i = 1; i <= ans.s[0]; i++) { if (i <= s[0]) sum += s[i]; if (i <= c.s[0]) sum += c.s[i]; ans.s[i] = sum % 10; sum /= 10; } while (sum) { ans.s[++ans.s[0]] = sum % 10; sum /= 10; } return ans; } bign operator * (const bign& c) { bign ans; for (int i = 1; i <= c.s[0]; i++) { int d = c.s[i],sum = 0; for (int j = 1; j <= s[0]; j++) { sum = d * s[j] + sum + ans.s[i + j - 1]; ans.s[i + j - 1] = sum % 10; sum /= 10; } int t = s[0] + i - 1; while (sum) { ++t; sum += ans.s[t]; ans.s[t] = sum % 10; sum /= 10; } ans.s[0] = max(ans.s[0],t); } return ans; } bign operator - (const bign& c) { bign ans = *this; for (int i = 1; i <= c.s[0]; i++) { if (ans.s[i] < c.s[i]) { ans.s[i] += 10; ans.s[i + 1] -= 1; } ans.s[i] -= c.s[i]; } ans.del(); return ans; } int operator / (const bign& c) { int ans = 0; bign d = *this; while (d >= c) { d = d - c; ans++; } return ans; } void put() { if (s[0] == 0) printf("0"); else for (int i = s[0]; i; i--) printf("%d",s[i]); } bign mul(int d) { s[0] += d; for (int i = s[0]; i > d; i--) s[i] = s[i - d]; for (int i = d; i; i--) s[i] = 0; return *this; } void del() { while (s[s[0]] == 0) { s[0]--; if (s[0] == 0) break; } } }; void Sqrt(char *num) { int len = strlen(num),i; bign ans = 0,b,c,d; if (len % 2) { i = 1; int cur = num[0] - '0'; ans = (int)sqrt(cur); d = cur; } else { int cur = (num[0] - '0') * 10 + num[1] - '0'; ans = (int)sqrt(cur); d = cur; i = 2; } if (i < len) { int cur = (num[i] - '0') * 10 + num[i + 1] - '0'; i += 2; c = cur; d = d.mul(2) + c; c = ans * ans; c = d - c.mul(2); b = ans + ans; b.mul(1); cur = c / b; while (1) { bign t = cur * cur; b = ans * (20 * cur) + t; b.del(); if (b <= c) { d = c - b; t = cur; ans = ans.mul(1) + t; break; } else if (cur) { cur--; } else break; } } while (i < len) { int cur = (num[i] - '0') * 10 + num[i + 1] - '0'; i += 2; c = cur; d = d.mul(2) + c; b = ans + ans; b.mul(1); cur = d / b; while (1) { bign t = cur * cur; b = ans * (20 * cur) + t; b.del(); if (b <= d) { d = d - b; t = cur; ans = ans.mul(1) + t; break; } else if (cur) { cur--; } else break; } } ans.put(); printf("n"); } int main () { #ifdef LOCAL freopen("in.txt","r",stdin); #endif int ncase; char num[maxn]; scanf("%d",&ncase); while (ncase--) { scanf("%s",num); Sqrt(num); if (ncase) printf("n"); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |