【模板】高精度 [高精度]

相当于把各种运算人工模拟一遍,还是很好理解的

参照的是这个dalao的模板   (感觉全机房的都用的这个模板)理解了就差不多了

一些我犯过的错误还有注意的地方

  • 小于的比较要从高位到低位比较
 1 #define rg register
 2 const int power=4,base=10000,N=10005;//万位
 3 char a[N],b[N];
 4 
 5 struct num{
 6     int a[N];
 7     num(){memset(a,0,sizeof(a));}
 8     num(char *s)
 9     {
10         memset(a,0,sizeof(a));
11         int len=strlen(s);
12         a[0]=(len+power-1)/power;
13         for(rg int i=0,t=0,w;i<len;++i,w*=10)
14         {
15             if(i%power==0) w=1,++t;
16             a[t]+=w*(s[i]-'0');
17         }
18     }
19     void print()
20     {
21         printf("%d",a[a[0]]);
22         for(rg int i=a[0]-1;i>0;--i) printf("%04d",a[i]);
23     }
24 }p,q,ans;
25 
26 num operator +(const num &p,const num &q)
27 {
28     num c;
29     c.a[0]=max(p.a[0],q.a[0]);
30     for(rg int i=1;i<=c.a[0];++i)
31     {
32         c.a[i]+=p.a[i]+q.a[i];
33         c.a[i+1]+=c.a[i]/base,c.a[i]%=base;
34     }
35     if(c.a[c.a[0]+1]) ++c.a[0];
36     return c;
37 }
38 
39 bool operator < (const num &p,const num &q)
40 {
41     if(p.a[0]<q.a[0]) return true;
42     if(p.a[0]>q.a[0]) return false;
43     for(rg int i=p.a[0];i>0;--i)
44     if(p.a[i]!=q.a[i]) return p.a[i]<q.a[i];
45     return false;
46 }
47 
48 num operator -(const num &p,const num &q)
49 {
50     num c=p;
51     for(rg int i=1;i<=c.a[0];++i)
52     {
53         c.a[i]-=q.a[i];
54         if(c.a[i]<0) c.a[i]+=base,--c.a[i+1];
55     }
56     while(c.a[0]>0&&!c.a[c.a[0]]) --c.a[0];
57     return c;
58 }
59 
60 num operator *(const num &p,const num &q)
61 {
62     num c;
63     c.a[0]=p.a[0]+q.a[0]-1;
64     for(rg int i=1;i<=p.a[0];++i)
65     for(rg int j=1;j<=q.a[0];++j)
66     {
67         c.a[i+j-1]+=p.a[i]*q.a[j];
68         c.a[i+j]+=c.a[i+j-1]/base,c.a[i+j-1]%=base;
69     }
70     if(c.a[c.a[0]+1]) ++c.a[0];
71     return c;
72 }

主模块

1 scanf("%s%s",&a,&b);
2 reverse(a,a+strlen(a));
3 reverse(b,b+strlen(b));//反转!!! 
4 p=num(a),q=num(b);

注意要反转 讲的时候没认真听 然后自己打的时候用的1位的数来测的 一交 “WA!” QAQ!!!

后面没看标准模板自己瓜想最终意识到要反转QAQ!!!

还要注意!的优先级   所以这件事告诉我们要自己多造几组数据,大一点的数据来检查!!!

然后就是各种模板(就是luogu训练场的一个任务)

A+B  A*B  A-B  数楼梯  B进制  

原文地址:https://www.cnblogs.com/lxyyyy/p/10736013.html