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

大数模板

时间:2014-09-30 15:07:09      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   ar   for   sp   div   

bubuko.com,布布扣
 1 struct BigNum{
 2         #define maxlen 1000
 3         #define memc(a, b) memcpy(a, b, sizeof(b))
 4         #define mem0(a) memset(a, 0, sizeof(a))
 5         typedef __int64 Num[maxlen + 2];
 6         Num num;
 7         BigNum operator+(BigNum num2) {
 8                 BigNum ans;
 9                 mem0(ans.num);
10                 for(int i = 1; i <= maxlen; i++) {
11                         ans.num[i] += num[i] + num2.num[i];
12                         ans.num[i + 1] += ans.num[i] / (int)1e9;
13                         ans.num[i] %= (int)1e9;
14                 }
15                 return ans;
16         }
17         BigNum operator*(BigNum num2) {
18                 BigNum ans;
19                 mem0(ans.num);
20                 for(int i = 1; i <= maxlen; i++) {
21                         for(int j = 1; j <= maxlen; j++) {
22                                 if(i + j - 1 <= maxlen) {
23                                         ans.num[i + j - 1] += num[i] * num2.num[j];
24                                         ans.num[i + j] += ans.num[i + j - 1] / (int)1e9;
25                                         ans.num[i + j - 1] %= (int)1e9;
26                                 }
27                         }
28                 }
29                 return ans;
30         }
31         void inp() {
32                 char s[maxlen + 2];
33                 mem0(num);
34                 scanf("%s", s);
35                 int len = strlen(s), cnt = 0;
36                 for(int i = len - 1; i >= 0; i -= 9) {
37                         int p = 0, x = 0, t = 1;
38                         while(i - p >= 0 && p < 9) {
39                                 x += t * (s[i - p] - 0);
40                                 p++;
41                                 t *= 10;
42                         }
43                         num[++cnt] = x;
44                 }
45         }
46         void outp() {
47                 int p = 1;
48                 for(int i = maxlen; i >= 1; i--) {
49                         if(num[i]) {
50                                 p = i;
51                                 break;
52                         }
53                 }
54                 cout<< num[p];
55                 while(--p) {
56                         int a[9] = {0}, x = num[p];
57                         for(int i = 0; i < 9; i++) {
58                                 a[i] = x % 10;
59                                 x /= 10;
60                         }
61                         for(int i = 8; i >= 0; i--) {
62                                 printf("%d", a[i]);
63                         }
64                 }
65         }
66 };
View Code

 

大数模板

标签:style   blog   http   color   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/jklongint/p/4001729.html

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