【高精度】【高精度四则运算模板】高精度除法

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 1000
struct hp{
    int a[MAXN+10];
    hp(){
        memset(a,0,sizeof a);
        a[0]=1;
    }
    hp(int n){
        memset(a,0,sizeof a);
        a[0]=0;
        while(n){
            a[++a[0]]=n%10;
            n/=10;
        }
        if(!a[0])
            a[0]=1;
    }
    hp(char *s){
        memset(a,0,sizeof a);
        int len=strlen(s);
        for(int i=1;i<=len;i++)
            a[i]=s[len-i]-'0';
        a[0]=len;
    }
    hp operator*(const hp &b)const{
        hp c;
        int i,j,len=a[0]+b.a[0];
        for(i=1;i<=a[0];i++)
            for(j=1;j<=b.a[0];j++)
                c.a[i+j-1]+=a[i]*b.a[j];
        for(i=1;i<len;i++){
            c.a[i+1]+=c.a[i]/10;
            c.a[i]%=10;
        }
        while(len>1&&!c.a[len])
            len--;
        c.a[0]=len;
        return c;
    }
    hp operator/(int b)const{
        hp c;
        int d=0,i,len=a[0];
        for(i=a[0];i;i--){
            d=d*10+a[i];
            c.a[i]=d/b;
            d%=b;
        }
        while(len>1&&!c.a[len])
            len--;
        c.a[0]=len;
        return c;
    }
    hp operator+(const hp &b)const{
        hp c;
        int len=max(a[0],b.a[0]),i;
        for(i=1;i<=len;i++){
            c.a[i]+=a[i]+b.a[i];
            c.a[i+1]=c.a[i]/10;
            c.a[i]%=10;
        }
        len++;
        while(len>1&&!c.a[len])
            len--;
        c.a[0]=len;
        return c;
    }
    hp operator-(const hp &b)const{
        hp c;
        int i,len=a[0];
        for(i=1;i<=len;i++){
            c.a[i]+=a[i]-b.a[i];
            if(c.a[i]<0)
                c.a[i]+=10,c.a[i+1]--;
        }
        while(len>1&&!c.a[len])
            len--;
        c.a[0]=len;
        return c;
    }
    void operator*=(const hp &x){
        *this=*this*x;
    }
    void operator/=(const int &x){
        *this=*this/x;
    }
    void operator+=(const hp &x){
        *this=*this+x;
    }
    void operator-=(const hp &x){
        *this=*this-x;
    }
    void print(){
        for(int i=a[0];i;i--)
            printf("%d",a[i]);
    }
    bool operator>(const hp&b)const{
        if(a[0]>b.a[0])
            return 1;
        if(a[0]<b.a[0])
            return 0;
        for(int i=a[0];i;i--)
            if(a[i]>b.a[i])
                return 1;
            else if(a[i]<b.a[i])
                return 0;
        return 0;
    }
    bool operator<(const hp&b)const{
        if(a[0]<b.a[0])
            return 1;
        if(a[0]>b.a[0])
            return 0;
        for(int i=a[0];i;i--)
            if(a[i]<b.a[i])
                return 1;
            else if(a[i]>b.a[i])
                return 0;
        return 0;
    }
    bool operator<=(const hp&b)const{
        return !(*this>b);
    }
    hp operator/(const hp&b)const{
        hp l(0),r(*this),mid;
        while(l<r){
            mid=(l+r+1)/2;
            if(mid*b<=*this)
                l=mid;
            else
                r=mid-1;
        }
        return l;
    }
    void operator/=(const hp&b){
        *this=*this/b;
    }
}a,b,c;
char s[MAXN+10];
void read(){
    scanf("%s",s);
    a=s;
    scanf("%s",s);
    b=s;
}
int main()
{
    read();
    c=a/b;
    c.print();
    puts("");
    a-=c*b;
    a.print();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值