码迷,mamicode.com
首页 > 其他好文 > 详细

模板 高精度运算

时间:2018-11-07 11:18:06      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:algorithm   str   pre   class   min   print   val   lse   using   

联赛前终于把高精度的坑填上了

读入写错了导致第一交爆5了..

贴上丑陋的压4位代码

  1 #include <queue>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #define N 26000
  6 #define maxn 10000
  7 #define inf 66666666
  8 #define idx(x) (x-‘0‘)
  9 using namespace std;
 10 
 11 int n,cte,num;
 12 struct bignum{
 13     int a[N],neg,len,res;
 14     friend bignum operator - (bignum &a,bignum &b);
 15     friend bignum operator + (bignum &a,bignum &b);
 16     friend int operator != (const bignum &a,const bignum &b)
 17     {
 18         if(a.len!=b.len) return 1;
 19         for(int i=1;i<=min(a.len,b.len);i++)
 20             if(a.a[i]!=b.a[i]) return 1;
 21         return 0; 
 22     }
 23     friend int operator <= (const bignum &a,const bignum &b)
 24     {
 25         if(a.len>b.len) return 0;
 26         if(b.len>a.len) return 1;
 27         for(int i=a.len;i>=1;i--)
 28             if(a.a[i]>b.a[i]) return 0;
 29             else if(a.a[i]<b.a[i]) return 1;
 30         return 1;
 31     }
 32     friend bignum operator + (bignum &a,bignum &b)
 33     {
 34         if(a.neg&&!b.neg){
 35             a.neg^=1;
 36             return b-a;   
 37         }else if(!a.neg&&b.neg){
 38             b.neg^=1;
 39             return a-b;
 40         }
 41         bignum ret;
 42         memset(&ret,0,sizeof(ret));
 43         if(a.neg&&b.neg) ret.neg^=1;
 44         ret.len=max(a.len,b.len);
 45         for(int i=1;i<=ret.len;i++)
 46         {
 47             ret.a[i]+=a.a[i]+b.a[i];
 48             if(ret.a[i]>=maxn) 
 49                 ret.a[i]-=maxn,ret.a[i+1]++;
 50         }
 51         if(ret.a[ret.len+1]!=0) ret.len++;
 52         while(!ret.a[ret.len])
 53             ret.len--;
 54         return ret;
 55     }
 56     friend bignum operator - (bignum &a,bignum &b)
 57     {   
 58         if(a.neg&&!b.neg){
 59             b.neg^=1;
 60             return a+b;   
 61         }else if(!a.neg&&b.neg){
 62             b.neg^=1;
 63             return a+b;
 64         }
 65         bignum ret;
 66         memset(&ret,0,sizeof(ret));
 67         if(b<=a) ret.neg=0;
 68         else swap(a,b),ret.neg^=1;
 69         if(a.neg&&b.neg) 
 70             swap(a,b),ret.neg^=1;
 71         ret.len=a.len;
 72         for(int i=1;i<=a.len;i++)
 73         {
 74             ret.a[i]+=a.a[i]-b.a[i];
 75             if(ret.a[i]<0) 
 76                 ret.a[i+1]--,ret.a[i]+=maxn;
 77         }
 78         while(!ret.a[ret.len])
 79             ret.len--;
 80         return ret;
 81     } 
 82     friend bignum operator * (bignum &a,bignum &b)
 83     {
 84         bignum ret;
 85         memset(&ret,0,sizeof(ret));
 86         if(a.neg) ret.neg^=1;
 87         if(b.neg) ret.neg^=1;
 88         for(int i=1;i<=a.len;i++)
 89             for(int j=1;j<=b.len;j++){
 90                 ret.a[i+j-1]+=a.a[i]*b.a[j];
 91                 if(ret.a[i+j-1]>=maxn){
 92                     ret.a[i+j]+=ret.a[i+j-1]/maxn;
 93                     ret.a[i+j-1]%=maxn;}
 94                 }
 95         ret.len=a.len+b.len;
 96         while(!ret.a[ret.len])
 97             ret.len--;
 98         return ret;
 99     }
100     friend bignum operator / (bignum &a,int b)
101     {
102         bignum ret;
103         memset(&ret,0,sizeof(ret));
104         //memset(&ret,&x,sizeof(x));
105         int sum=0;
106         for(int i=a.len;i>=1;i--){
107             sum=sum*maxn+a.a[i];
108             ret.a[i]=sum/b;
109             sum%=b;
110         }ret.len=a.len,ret.res=sum;
111         while(!ret.a[ret.len]&&ret.len>1)
112             ret.len--;
113         return ret;
114     }
115 };
116 char str[N*4];
117 bignum read()
118 {
119     scanf("%s",str);
120     bignum ret;
121     int mi=0;
122     memset(&ret,0,sizeof(ret));
123     if(str[0]==-) ret.neg=1,mi++;
124     int len=strlen(str);
125     ret.len=1;
126     int cnt=1,pw=1;
127     for(int i=len-1;i>=mi;i--)
128     {   
129         if(cnt==5) 
130             cnt=1,pw=1,ret.len++;
131         ret.a[ret.len]+=idx(str[i])*pw;
132         cnt++,pw*=10;
133     }
134     return ret;
135 }
136 
137 int main()
138 {
139     scanf("%d",&n);
140     bignum ret,a,b;
141     int res,val;
142     if(n==1){
143         a=read(),b=read();
144         ret=a+b;
145     }else if(n==2){
146         a=read(),b=read();
147         ret=a-b;
148     }else if(n==3){
149         a=read(),b=read();
150         ret=a*b;
151     }else{
152         a=read();
153         scanf("%d",&val);
154         ret=a/val;
155     }
156     if(ret.neg)
157         printf("-");
158     printf("%d",ret.a[ret.len]);
159     for(int i=ret.len-1;i>=1;i--)
160         printf("%04d",ret.a[i]);
161     puts("");
162     if(n==4) printf("%d\n",ret.res);
163     return 0;
164 }

 

模板 高精度运算

标签:algorithm   str   pre   class   min   print   val   lse   using   

原文地址:https://www.cnblogs.com/guapisolo/p/9920539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!