高精度

struct gao{
    int cnt,a[N];
    gao(){memset(a,0,sizeof(a));cnt=0;}
    gao operator + (const gao &b)const{
        gao c;
        int yu=0;
        c.cnt=max(cnt,b.cnt);
        for(int i=1;i<=c.cnt;i++) c.a[i]=a[i]+b.a[i]+yu,yu=c.a[i]/10,c.a[i]%=10;
        if(yu) c.cnt++,c.a[c.cnt]=yu;
        return c;
    }
    gao operator - (const gao &b)const{
        gao c;
        int yu=0;
        c.cnt=max(cnt,b.cnt);
        for(int i=1;i<=c.cnt;i++){
            c.a[i]=a[i]-b.a[i]-yu;
            if(c.a[i]<0) c.a[i]+=10,yu=1;
            else yu=0;
        }
        if(yu) c.cnt++,c.a[c.cnt]=yu;
        return c;
    }
    gao operator * (const gao &b)const{
        gao c;
        c.cnt=cnt+b.cnt;
        for(int i=1;i<=cnt;i++){
            for(int j=1;j<=b.cnt;j++){
                c.a[i+j-1]+=a[i]*b.a[j];
                c.a[i+j]+=c.a[i+j-1]/10,c.a[i+j-1]%=10;
            }
        }
        while(!c.a[c.cnt]&&c.cnt>1) c.cnt--;
        return c;
    }
    gao mid(){
        gao c;
        c.cnt=cnt;
        for(int i=cnt,yu=0;i>=1;i--){
            c.a[i]=(a[i]+yu*10)/2,yu=(a[i]+yu)%2;
        }
        while(!c.a[c.cnt]&&c.cnt>1) c.cnt--;
        return c;
    }
    bool operator <= (const gao &b) const{
        if(cnt<b.cnt) return 1;
        if(cnt>b.cnt) return 0;
        for(int i=cnt;i>=1;i--){
            if(a[i]<b.a[i]) return 1;
            if(a[i]>b.a[i]) return 0;
        }
        return 1;
    }
    void pr(){
        for(int i=cnt;i>=1;i--) printf("%d",a[i]);
    }
}
posted @ 2022-10-27 14:05  hubingshan  阅读(26)  评论(0)    收藏  举报  来源