模板 高精度

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<queue>
  5 #define ms(a,b) memset(a,b,sizeof a)
  6 #define rep(i,a,n) for(int i = a;i <= n;i++)
  7 #define per(i,n,a) for(int i = n;i >= a;i--)
  8 #define inf 1000000007
  9 using namespace std;
 10 typedef long long ll;
 11 typedef double D;
 12 #define eps 1e-8
 13 ll read() {
 14     ll as = 0,fu = 1;
 15     char c = getchar();
 16     while(c < ‘0‘ || c > ‘9‘) {
 17         if(c == ‘-‘) fu = -1;
 18         c = getchar();
 19     }
 20     while(c >= ‘0‘ && c <= ‘9‘) {
 21         as = as * 10 + c - ‘0‘;
 22         c = getchar();
 23     }
 24     return as * fu;
 25 }
 26 struct Big {
 27     const static int N = 5005;
 28     int a[N];
 29     bool flag;
 30     Big(){ms(a,0),flag = 0;}
 31     Big( ll x ){
 32         ms(a,0),flag = 0;
 33         flag = (x < 0);
 34         x = max(x,-x);
 35         while(x) a[++a[0]] = x%10,x/=10;
 36         clr0();
 37     }
 38     void read() {
 39         ms(a,0),flag = 0;
 40         char s[N];
 41         scanf("%s",s+1);
 42         a[0] = strlen(s+1);
 43         if(s[1] == ‘-‘) a[0]--,flag = 1;
 44         rep(i,1,a[0]) a[i] = s[a[0] - i + flag + 1] - ‘0‘;
 45         clr0();
 46     }
 47     void clr0() {
 48         while(a[0] && a[a[0]] == 0) a[0]--;
 49         while(a[0] < 0) a[0]++;
 50         if(a[0] == 0) flag = 0;
 51     }
 52     void print() {
 53         clr0();
 54         if(!a[0]) return void(puts("0"));
 55         if(flag) putchar(‘-‘);
 56         per(i,a[0],1) putchar(a[i] + ‘0‘);
 57         putchar(‘\n‘);
 58     }
 59     //clr0 before use
 60     bool operator < (const Big &o) const {
 61         if(o.a[0] == 0) return flag;
 62         if(a[0] == 0) return !o.flag;
 63         if(flag ^ o.flag) return flag;
 64         if(flag) {
 65             rep(i,0,a[0]) {
 66                 if(a[i] > o.a[i]) return 1;
 67                 if(a[i] < o.a[i]) return 0;
 68             }
 69             return 0;
 70         } else {
 71             rep(i,0,a[0]) {
 72                 if(a[i] < o.a[i]) return 1;
 73                 if(a[i] > o.a[i]) return 0;
 74             }
 75             return 0;
 76         }
 77     }
 78     bool operator == (const Big &o) const {
 79         Big r = *this;
 80         return !(r < o || o < r);
 81     }
 82     //保证同号
 83     Big operator + (const Big &o) const {
 84         if(a[0] == 0) return o;
 85         if(o.a[0] == 0) return *this;
 86         if(flag ^ o.flag) {
 87             Big x = *this,y = o;
 88             if(x.flag) {
 89                 x.flag = 0;
 90                 return y - x;
 91             }
 92             else {
 93                 y.flag = 0;
 94                 return x - y;
 95             }
 96         }
 97         Big ans;
 98         ms(ans.a,0);
 99         ans.a[0] = max(a[0],o.a[0]),ans.flag = flag;
100         rep(i,1,ans.a[0]) {
101             ans.a[i] += a[i] + o.a[i];
102             if(i == ans.a[0] && ans.a[i] >= 10) {
103                 ans.a[0]++;
104             }
105             ans.a[i+1] += ans.a[i] / 10;
106             ans.a[i] %= 10;
107         }
108         return ans;
109     }
110     //保证同号
111     Big operator - (const Big &o) const {
112         Big x = *this;
113         Big y = o;
114         if(flag ^ o.flag) {
115             y.flag ^= 1;
116             return x + y;
117         }
118         Big ans;
119         ms(ans.a,0);
120         ans.a[0] = ans.flag = 0;
121         ans.flag = flag;
122         x.flag = y.flag = 0;
123         if(x == y) return ans;
124         if(x < y) swap(x,y),ans.flag ^= 1;
125         rep(i,1,x.a[0]) {
126             if(x.a[i] < y.a[i]) x.a[i] += 10,x.a[i+1]--;
127             ans.a[i] = x.a[i] - y.a[i];
128         }
129         ans.a[0] = x.a[0];
130         ans.clr0();
131         return ans;
132     }
133     //O(n^2) 高精乘
134     Big operator * (const Big &o) const {
135         if(a[0] == 0) return *this;
136         if(o.a[0] == 0) return o;
137         Big ans;
138         ms(ans.a,0);
139         ans.a[0] = a[0] + o.a[0],ans.flag = o.flag ^ flag;
140         rep(i,1,a[0]) rep(j,1,o.a[0])
141             ans.a[i+j-1] += a[i] * o.a[j];
142         rep(i,1,ans.a[0]) {
143             if(i == ans.a[0] && ans.a[i] >= 10) ans.a[0]++;
144             ans.a[i+1] += ans.a[i] / 10;
145             ans.a[i] %= 10;
146         }
147         return ans;
148     }
149 }x,y,z;
150 Big zero = Big(0);
151 Big one = Big(1);
152 Big two = Big(2);
153 Big three = Big(3);
154 #define Max(a,b) ((b)<(a)?(a):(b))
155 #define Min(a,b) ((a)<(b)?(a):(b))
156 void tst() {
157     while(1) {
158         x.read(),y.read();
159         z = x + y;
160         printf("plus:"),z.print();
161         z = x - y;
162         printf("minus:"),z.print();
163         z = x * y;
164         printf("mult:"),z.print();
165     }
166 }

今天终于写完了高精真开心

热烈欢迎大家Hack

提供数据 [email protected]

原文地址:https://www.cnblogs.com/yuyanjiaB/p/9895512.html

时间: 2024-08-05 20:30:45

模板 高精度的相关文章

模板 高精度大整数

#include<bits/stdc++.h> #define clr(x,y) memset((x),(y),sizeof(x)) using namespace std; typedef long long LL; const int MaxL=400; //大数长度 //大整数 //减法只能大减小 struct bign { int len, s[MaxL]; //构造函数 bign () { memset(s, 0, sizeof(s)); len = 1; } bign (int n

模板 高精度 转自郭爷

//http://www.cnblogs.com/HarryGuo2012/p/4524041.html #include<string> #include<iostream> #include<iosfwd> #include<cmath> #include<cstring> #include<stdlib.h> #include<stdio.h> #include<cstring> #define MAX_

模板库

read()+print() inline int read() { char ch; bool bj=0; while(!isdigit(ch=getchar())) bj|=(ch=='-'); int res=ch^(3<<4); while(isdigit(ch=getchar())) res=(res<<1)+(res<<3)+(ch^(3<<4)); return bj?-res:res; } void printnum(int x) { if(

[Template]高精度模板

重新写一下高精度模板(不要问我为什么) 自认为代码风格比较漂亮(雾 如果有更好的写法欢迎赐教 封装结构体big B是压位用的进制,W是每位长度 size表示长度,d[]就是保存的数字,倒着保存,从1开始 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const i

【模板】高精度

我之前一直觉得高精度很麻烦,然而今天一打才发现 这么个zz东西我居然还弃疗过(模板果然还是要自己打) 高精度加法 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 int lena,lenb,tot; 7 char a[505],b[505]; 8 int aa[505],bb[505],c[50

大数高精度运算(模板)

前言:高精度运算.是指參与运算的数(加数.减数,因子--)范围大大超出了标准数据类型(整型,实型)能表示的范围的运算. 模板:包含大数加减乘除.大数与int数的乘法,模板能够不断扩充. 代码: /* 所有亲測可用,可是不能用于负数的运算,仅仅能对正数进行大数运算 */ const int ten[4]= {1,10,100,1000}; const int maxl = 300; struct BigNumber { int d[maxl]; char s[maxl]; BigNumber(co

poj1220:高精度进制转换模板题

今天撸3708  一直奇怪的re 就先放下了,写这个题的过程中学习了一个高精度进制转换,用这个模板写了1220 记录一下: #include <iostream> #include <stdio.h> #include<string.h> #include<algorithm> #include<string> #include<ctype.h> using namespace std; #define MAXN 10000 char

【原创】高精度(压位储存)模板

无聊写了个高精度模板玩玩...... 1 /* 2 高精度(压位储存) 3 */ 4 #include <cstdlib> 5 #include <iostream> 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 const int MAX=10005;//最长长度 10 using namespace std; 11 //高精度结构体,先声明后实现 12 struct

高精度模板(修改中)

这个是一个高精度的模板,还在完成中... 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cmath> 6 #include<cstring> 7 #include<vector> 8 #define xh(a,b,c)for(int a=(b);a<=(c);a++) 9 #de