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

大数处理 详解 模版

时间:2016-05-21 21:49:37      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<math.h>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<queue>
  7 #include<vector>
  8 #include<set>
  9 #include<stack>
 10 #include<string>
 11 #include<sstream>
 12 #include<map>
 13 #include<cctype>
 14 #include<limits.h>
 15 using namespace std;
 16 char str[100000001];
 17 int modd;
 18 #define MAXN 9999
 19 #define MAXSIZE 10
 20 #define DLEN                    //  每个 int  单元内储存的 位数 
 21 class BigNum
 22 {
 23 private:
 24     int a[1010];    //可以控制大数的位数
 25     int len;       //大数长度
 26 public:
 27     BigNum(){ len = 1;memset(a,0,sizeof(a)); }   //构造函数
 28     BigNum(const int);       //将一个int类型的变量转化为大数
 29     BigNum(const char*);     //将一个字符串类型的变量转化为大数
 30     BigNum(const BigNum &);  //拷贝构造函数
 31     BigNum &operator=(const BigNum &);   //重载赋值运算符,大数之间进行赋值运算
 32 
 33     friend istream& operator>>(istream&,  BigNum&);   //重载输入运算符
 34     friend ostream& operator<<(ostream&,  BigNum&);   //重载输出运算符
 35 
 36     BigNum operator+(const BigNum &) const;   //重载加法运算符,两个大数之间的相加运算
 37     BigNum operator-(const BigNum &) const;   //重载减法运算符,两个大数之间的相减运算
 38     BigNum operator*(const BigNum &) const;   //重载乘法运算符,两个大数之间的相乘运算
 39     BigNum operator/(const int   &) const;    //重载除法运算符,大数对一个整数进行相除运算
 40 
 41     BigNum operator^(const int  &) const;    //大数的n次方运算
 42     int    operator%(const int  &) const;    //大数对一个int类型的变量进行取模运算
 43     bool   operator>(const BigNum & T)const;   //大数和另一个大数的大小比较
 44     bool   operator>(const int & t)const;      //大数和一个int类型的变量的大小比较
 45 
 46     void print();       //输出大数
 47 };
 48 BigNum::BigNum(const int b)     //将一个int类型的变量转化为大数
 49 {
 50     int c,d = b;
 51     len = 0;
 52     memset(a,0,sizeof(a));
 53     while(d > MAXN)
 54     {
 55         c = d - (d / (MAXN + 1)) * (MAXN + 1);
 56         d = d / (MAXN + 1);
 57         a[len++] = c;
 58     }
 59     a[len++] = d;
 60 }
 61 BigNum::BigNum(const char*s)     //将一个字符串类型的变量转化为大数
 62 {
 63     int t,k,index,l,i;
 64     memset(a,0,sizeof(a));
 65     l=strlen(s);
 66     len=l/DLEN;
 67     if(l%DLEN)
 68         len++;
 69     index=0;
 70     for(i=l-1;i>=0;i-=DLEN)
 71     {
 72         t=0;
 73         k=i-DLEN+1;
 74         if(k<0)
 75             k=0;
 76         for(int j=k;j<=i;j++)
 77             t=t*10+s[j]-0;
 78         a[index++]=t;
 79     }
 80 }
 81 BigNum::BigNum(const BigNum & T) : len(T.len)  //拷贝构造函数
 82 {
 83     int i;
 84     memset(a,0,sizeof(a));
 85     for(i = 0 ; i < len ; i++)
 86         a[i] = T.a[i];
 87 }
 88 BigNum & BigNum::operator=(const BigNum & n)   //重载赋值运算符,大数之间进行赋值运算
 89 {
 90     int i;
 91     len = n.len;
 92     memset(a,0,sizeof(a));
 93     for(i = 0 ; i < len ; i++)
 94         a[i] = n.a[i];
 95     return *this;
 96 }
 97 istream& operator>>(istream & in,  BigNum & b)   //重载输入运算符
 98 {
 99     char ch[MAXSIZE*4];
100     int i = -1;
101     in>>ch;
102     int l=strlen(ch);
103     int count=0,sum=0;
104     for(i=l-1;i>=0;)
105     {
106         sum = 0;
107         int t=1;
108         for(int j=0;j<4&&i>=0;j++,i--,t*=10)
109         {
110             sum+=(ch[i]-0)*t;
111         }
112         b.a[count]=sum;
113         count++;
114     }
115     b.len =count++;
116     return in;
117 
118 }
119 ostream& operator<<(ostream& out,  BigNum& b)   //重载输出运算符
120 {
121     int i;
122     cout << b.a[b.len - 1];
123     for(i = b.len - 2 ; i >= 0 ; i--)
124     {
125         cout.width(DLEN);
126         cout.fill(0);
127         cout << b.a[i];
128     }
129     return out;
130 }
131 
132 BigNum BigNum::operator+(const BigNum & T) const   //两个大数之间的相加运算
133 {
134     BigNum t(*this);
135     int i,big;      //位数
136     big = T.len > len ? T.len : len;
137     for(i = 0 ; i < big ; i++)
138     {
139         t.a[i] +=T.a[i];
140         if(t.a[i] > MAXN)
141         {
142             t.a[i + 1]++;
143             t.a[i] -=MAXN+1;
144         }
145     }
146     if(t.a[big] != 0)
147         t.len = big + 1;
148     else
149         t.len = big;
150     return t;
151 }
152 BigNum BigNum::operator-(const BigNum & T) const   //两个大数之间的相减运算
153 {
154     int i,j,big;
155     bool flag;
156     BigNum t1,t2;
157     if(*this>T)
158     {
159         t1=*this;
160         t2=T;
161         flag=0;
162     }
163     else
164     {
165         t1=T;
166         t2=*this;
167         flag=1;
168     }
169     big=t1.len;
170     for(i = 0 ; i < big ; i++)
171     {
172         if(t1.a[i] < t2.a[i])
173         {
174             j = i + 1;
175             while(t1.a[j] == 0)
176                 j++;
177             t1.a[j--]--;
178             while(j > i)
179                 t1.a[j--] += MAXN;
180             t1.a[i] += MAXN + 1 - t2.a[i];
181         }
182         else
183             t1.a[i] -= t2.a[i];
184     }
185     t1.len = big;
186     while(t1.a[len - 1] == 0 && t1.len > 1)
187     {
188         t1.len--;
189         big--;
190     }
191     if(flag)
192         t1.a[big-1]=0-t1.a[big-1];
193     return t1;
194 }
195 
196 BigNum BigNum::operator*(const BigNum & T) const   //两个大数之间的相乘运算
197 {
198     BigNum ret;
199     int i,j,up;
200     int temp,temp1;
201     for(i = 0 ; i < len ; i++)
202     {
203         up = 0;
204         for(j = 0 ; j < T.len ; j++)
205         {
206             temp = a[i] * T.a[j] + ret.a[i + j] + up;
207             if(temp > MAXN)
208             {
209                 temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
210                 up = temp / (MAXN + 1);
211                 ret.a[i + j] = temp1;
212             }
213             else
214             {
215                 up = 0;
216                 ret.a[i + j] = temp;
217             }
218         }
219         if(up != 0)
220             ret.a[i + j] = up;
221     }
222     ret.len = i + j;
223     while(ret.a[ret.len - 1] == 0 && ret.len > 1)
224         ret.len--;
225     return ret;
226 }
227 BigNum BigNum::operator/(const int & b) const   //大数对一个整数进行相除运算
228 {
229     BigNum ret;
230     int i,down = 0;
231     for(i = len - 1 ; i >= 0 ; i--)
232     {
233         ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
234         down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
235     }
236     ret.len = len;
237     while(ret.a[ret.len - 1] == 0 && ret.len > 1)
238         ret.len--;
239     return ret;
240 }
241 int BigNum::operator %(const int & b) const    //大数对一个int类型的变量进行取模运算
242 {
243     int i,d=0;
244     for (i = len-1; i>=0; i--)
245     {
246         d = ((d * (MAXN+1))% b + a[i])% b;
247     }
248     return d;
249 }
250 BigNum BigNum::operator^(const int & n) const    //大数的n次方运算
251 {
252     BigNum t,ret(1);
253     int i;
254     if(n<0)
255         exit(-1);
256     if(n==0)
257         return 1;
258     if(n==1)
259         return *this;
260     int m=n;
261     while(m>1)
262     {
263         t=*this;
264         for( i=1;i<<1<=m;i<<=1)
265         {
266             t=t*t;
267         }
268         m-=i;
269         ret=ret*t;
270         if(m==1)
271             ret=ret*(*this);
272     }
273     return ret;
274 }
275 bool BigNum::operator>(const BigNum & T) const   //大数和另一个大数的大小比较
276 {
277     int ln;
278     if(len > T.len)
279         return true;
280     else if(len == T.len)
281     {
282         ln = len - 1;
283         while(a[ln] == T.a[ln] && ln >= 0)
284             ln--;
285         if(ln >= 0 && a[ln] > T.a[ln])
286             return true;
287         else
288             return false;
289     }
290     else
291         return false;
292 }
293 bool BigNum::operator >(const int & t) const    //大数和一个int类型的变量的大小比较
294 {
295     BigNum b(t);
296     return *this>b;
297 }
298 
299 void BigNum::print()    //输出大数
300 {
301     int i;
302     cout << a[len - 1];
303     for(i = len - 2 ; i >= 0 ; i--)
304     {
305         cout.width(DLEN);
306         cout.fill(0);
307         cout << a[i];
308     }
309     cout << endl;
310 }
311 int main(void)
312 {
313     while(~scanf("%s %d", str, &modd))
314     {
315         char a[10000],b=b;
316         BigNum big(str);
317         big=big^2;
318         big.print();
319         //printf("%s",a);
320     }
321     return 0;
322 }

 

大数处理 详解 模版

标签:

原文地址:http://www.cnblogs.com/A-FM/p/5515528.html

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