首页 > 代码库 > [Template]高精度模板

[Template]高精度模板

重新写一下高精度模板(不要问我为什么)

 

自认为代码风格比较漂亮(雾

如果有更好的写法欢迎赐教

 


 

封装结构体big

B是压位用的进制,W是每位长度

size表示长度,d[]就是保存的数字,倒着保存,从1开始

 

 


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int N=1005,B=1e4,W=4,L=1005;struct big{    int size,d[L];    big():size(0){memset(d,0,sizeof(d));}};//---------------------------------------------------bool cmpInt(big &a,int b){//a>=b    if(a.size>1) return true;    else if(a.d[1]>=b) return true;    return false;}void addInt(big &a,int b){    int t=a.d[1]+b,g=t/B;    a.d[1]=t%B;    for(int i=2;g;i++){//jin wei        t=a.d[i]+g;        a.d[i]=t%B;        g=t/B;        a.size=max(a.size,i);//update size    }}void mnuInt(big &a,int b){    if(a.d[1]<b){//jie wei        a.d[1]+=B;        int i;        for(i=2;a.d[i]==0;i++) a.d[i]+=B-1;        a.d[i]--;        while(a.d[a.size]==0) a.size--;//update size    }    a.d[1]-=b;//last minus}void mulInt(big &a,int b){    int g=0;    for(int i=1;i<=a.size;i++){//mul        int t=a.d[i]*b+g;        a.d[i]=t%B;        g=t/B;    }    while(g){//jin wei        a.d[++a.size]=g%B;//update size        g/=B;    }}void divInt(big &a,int b){    int g=0;    for(int i=a.size;i>=1;i--){//divide        g=g*B+a.d[i];        a.d[i]=g/b;        g%=b;    }    while(a.d[a.size]==0) a.size--;//update size}//---------------------------------------------------bool cmp(big &a,big &b){//a>b    if(a.size!=b.size) return a.size>b.size;    for(int i=1;i<=a.size;i++)        if(a.d[i]!=b.d[i]) return a.d[i]>b.d[i];    return false;//a==b}void add(big &a,big &b){//a=a+b    int g=0,i;    for(i=1;;i++){        if(g==0&&i>a.size&&i>b.size) break;        int t=g;        t+=i<=a.size?a.d[i]:0;        t+=i<=b.size?b.d[i]:0;        a.d[i]=t%B;        g=t/B;    }    a.size=i-1;//update size}void mnu(big &a,big &b){//a=a-b    for(int i=1;i<=b.size;i++){        if(a.d[i]<b.d[i]){            int p=i+1;            while(a.d[p]==0) p++;            a.d[p]--;            a.d[i]+=B;        }        a.d[i]-=b.d[i];    }    while(a.d[a.size]==0) a.size--;}void mul(big &a,big &b,big &c){//c=a*b    for(int i=1;i<=a.size;i++){        int g=0;        for(int j=1;j<=b.size;j++){            c.d[i+j-1]+=a.d[i]*b.d[j]+g;            g=c.d[i+j-1]/B;            c.d[i+j-1]%=B;        }        c.d[i+b.size]=g;    }    c.size=a.size+b.size;    while(c.d[c.size]==0) c.size--;}//---------------------------------------------------void scan(big &a,char s[]){//annoy B ,if B==10 u can directly use the string    int len=strlen(s+1);    int p=a.size=len/W+(len%W!=0);    for(int i=1;i<=p;i++){        int r=len-(i-1)*W,l=max(len-i*W+1,1);        for(int j=l;j<=r;j++) a.d[i]=a.d[i]*10+s[j]-0;    }}void print(big &a){    printf("%d",a.d[a.size]);    for(int i=a.size-1;i>=1;i--){        if(a.d[i]<10) printf("000");        else if(a.d[i]<100) printf("00");        else if(a.d[i]<1000) printf("0");        printf("%d",a.d[i]);    }    putchar(\n);}//---------------------------------------------------int main(){}

 

[Template]高精度模板