首页 > 其他 > 详细

重载运算符高精---高精乘除低精

时间:2021-02-05 09:47:25      阅读:26      评论:0      收藏:0      [点我收藏+]
ll p=100000000000000;
struct ac{
    ll x[100];
    friend ac operator + (ac a,ac b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=1;i<=a.x[0] || i<=b.x[0];i++){
            c.x[i]+=a.x[i]+b.x[i];
            c.x[i+1]=c.x[i]/p;
            c.x[i]%=p;
        }
        c.x[0]=max(a.x[0],b.x[0]);
        if(c.x[c.x[0]+1]!=0){
            c.x[0]++;
        }
        return c;
    }
    friend ac operator - (ac a,ac b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=1;i<=a.x[0];i++){
            if(a.x[i]<b.x[i]){
                a.x[i]+=p;
                a.x[i+1]--;
            }
            c.x[i]=a.x[i]-b.x[i];
            cout<<c.x[i]<<endl;
        }
        c.x[0]=a.x[0];
        while(c.x[c.x[0]]==0){
            c.x[0]--;
        }
        return c;
    }
    friend ac operator * (ac a,ll b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=1;i<=a.x[0];i++){
            c.x[i]+=a.x[i]*b;
            c.x[i+1]+=c.x[i]/p;    
            c.x[i]%=p;
        }
        c.x[0]=a.x[0];
        if(c.x[c.x[0]+1]!=0){
            c.x[0]++;
        }
        return c;
    }
    friend ac operator / (ac a,ll b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=a.x[0];i>=1;i--){
            c.x[i]=a.x[i]/b;
            a.x[i-1]+=a.x[i]%b*p;
        }
        c.x[0]=a.x[0];
        while(c.x[c.x[0]]==0){
            c.x[0]--;
        }
        return c;
    }
};
void print(ac a){
    printf("%lld",a.x[a.x[0]]);
    for(int i=a.x[0]-1;i>=1;i--){
        printf("%.14lld",a.x[i]);
    }
    printf("\n
#define ll long long
ll p=100000000000000;
struct ac{
    ll x[100];
    friend ac operator + (ac a,ac b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=1;i<=a.x[0] || i<=b.x[0];i++){
            c.x[i]+=a.x[i]+b.x[i];
            c.x[i+1]=c.x[i]/p;
            c.x[i]%=p;
        }
        c.x[0]=max(a.x[0],b.x[0]);
        if(c.x[c.x[0]+1]!=0){
            c.x[0]++;
        }
        return c;
    }
    friend ac operator - (ac a,ac b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=1;i<=a.x[0];i++){
            if(a.x[i]<b.x[i]){
                a.x[i]+=p;
                a.x[i+1]--;
            }
            c.x[i]=a.x[i]-b.x[i];
            cout<<c.x[i]<<endl;
        }
        c.x[0]=a.x[0];
        while(c.x[c.x[0]]==0){
            c.x[0]--;
        }
        return c;
    }
    friend ac operator * (ac a,ll b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=1;i<=a.x[0];i++){
            c.x[i]+=a.x[i]*b;
            c.x[i+1]+=c.x[i]/p;    
            c.x[i]%=p;
        }
        c.x[0]=a.x[0];
        if(c.x[c.x[0]+1]!=0){
            c.x[0]++;
        }
        return c;
    }
    friend ac operator / (ac a,ll b){
        ac c;
        memset(c.x,0,sizeof(c.x));
        for(int i=a.x[0];i>=1;i--){
            c.x[i]=a.x[i]/b;
            a.x[i-1]+=a.x[i]%b*p;
        }
        c.x[0]=a.x[0];
        while(c.x[c.x[0]]==0){
            c.x[0]--;
        }
        return c;
    }
};
void print(ac a){
    printf("%lld",a.x[a.x[0]]);
    for(int i=a.x[0]-1;i>=1;i--){
        printf("%.14lld",a.x[i]);
    }
    printf("\n");
}

 

重载运算符高精---高精乘除低精

原文:https://www.cnblogs.com/fengwu2005/p/14375863.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!