加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数模板

发布时间:2020-12-14 03:06:59 所属栏目:大数据 来源:网络整理
导读://大数模板 #includestdio.h #includequeue #includestring.h #includealgorithm using namespace std; const int SIZE = 7;// ?不允许更改 const int mod = 1000000000;// ?不允许更改 #define ll long long #define N ?1000 struct bignum ? ?// ? ?结构体
//大数模板 #include<stdio.h> #include<queue> #include<string.h> #include<algorithm> using namespace std; const int SIZE = 7;// ?不允许更改 const int mod = 1000000000;// ?不允许更改 #define ll long long #define N ?1000 struct bignum ? ?// ? ?结构体 ?重载大数操作 ?//* 特别提醒 *// 不支持负数操作 或产生负数的操作 ?也不支持分数操作 ?不同的操作的位数不同 乘法结果不大于 10^36 { long long a[SIZE]; bignum() { memset(a,sizeof(a)); } bignum(long long n) ?// ? 给该结构结构赋值 ? ?调用 ? ans1=bignum(n),ans2=bignum(m) ?; 等价 ans1=n,ans2=m; { memset(a,sizeof(a)); a[0] = n; adjust(); } void adjust()? { for (int i = 0; i < SIZE; i++) if (a[i] >= mod) { a[i + 1] += a[i] / mod; a[i] %= mod; } else if (a[i] < 0) { a[i + 1] --; a[i] += mod; } } bignum operator * (const bignum &p) ? // ? 结构与结构之间的乘法 ? ans1=bignum(n),ans2=bignum(m) ? 如 ?ans=ans1*ans2; ? 等价于 ans=n*m; { bignum c; for (int i = 0; i < SIZE; i++) for (int j = 0; i + j < SIZE; j++) c.a[i + j] += a[i] * p.a[j]; c.adjust(); return c; } bignum operator / (const int x) ? // ? 结构与 int 型数除法 ? ans1=bignum(n),m ? 如 ?ans=ans1/m; ? 等价于 ans=n/m; { long long tmp = 0; bignum c; memcpy(c.a,a,sizeof(a)); for (int i = SIZE - 1; i >= 0; i--)? { long long p = (tmp * mod + c.a[i]) % x; c.a[i] = (tmp * mod + c.a[i]) / x; tmp = p; } return c; } bignum operator + (const?bignum &p) ? // ?结构之间的加法 ? ans1=bignum(n),ans2=bignum(m) 如 ? ans=ans1+ans2 ; ? 等价于 ans=n+m; { bignum c; for (int i = 0; i < SIZE; i++) c.a[i] = a[i] + p.a[i]; c.adjust(); return c; } bignum operator - (const?bignum &p) ?// ?只允许大数 减小数 ?不允许负数和零 ?ans1=bignum(n),ans2=bignum(m) 如 ? ans=ans1+ans2 ; ? 等价于 ans=n+m; { bignum c; for (int i = 0; i < SIZE; i++) c.a[i] = a[i] - p.a[i]; c.adjust(); return c; } bool operator == (const?bignum &p) ? // ?结构之间相互赋值 ? ans1=bignum(n),ans2=bignum(m) 如 ? ans=ans1 ; ? 等价于 ans=n ?; { for (int i = 0; i < SIZE; i++) if (a[i] != p.a[i]) return false; return true; } void output() ? // ?输出结构中的数 { int i = SIZE - 1; for (i; i >= 0; i--)? { if (a[i]) break; } printf("%lld",a[i]); for (i = i - 1; i >= 0; i--) printf("%09lld",a[i]); printf("n"); } }; int main() { ll n,sum; while (scanf("%lld%lld",&n,&sum) != EOF) { bignum ans;? ans = bignum(0); ?// ?ans=0; ans = bignum(n) + bignum(sum); ans.output(); ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ans = bignum(n) - bignum(sum); ans.output(); ans = bignum(n) * bignum(sum); ans.output(); ans = bignum(n)/sum; // ?只满足整除的情况 ans.output(); } return 0; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读