XDOJ 1046 - 高精度模板综合测试 - [高精度模板]

 题目链接:http://acm.xidian.edu.cn/problem.php?id=1046

题目描述

请输出两个数的和,差,积,商,取余。注意不要有前导零。

输入

多组数据,每组数据是两个整数A,B(0<=A<=10^100,0<B<=10^100).

输出

对于每组数据,输出五个整数,分别代表A+B,A-B,A*B,A/B,A%B.

样例输入
5 2
11 3

样例输出
7 3 10 2 1
14 8 33 3 2

 

模板注释:

本模板只能处理十进制非负大整数。

 

AC代码:

#include<bits/stdc++.h>
using namespace std;

struct BigInt
{
    int len,d[205];

    void clean(){while(len>1 && !d[len-1]) len--;}
    string str()const
    {
        string s;
        for(int i=0;i<len;i++) s+=d[len-1-i]+'0';
        return s;
    }

    BigInt(){memset(d,0,sizeof(d));len=1;}
    BigInt(int num){*this=num;}
    BigInt(char* num){*this=num;}

    bool operator<(const BigInt& oth)const
    {
        if(len!=oth.len) return len<oth.len;
        for(int i=len-1;i>=0;i--) if(d[i]!=oth.d[i]) return d[i]<oth.d[i];
        return false;
    }
    bool operator>(const BigInt& oth)const{return oth<*this;}
    bool operator<=(const BigInt& oth)const{return !(oth<*this);}
    bool operator>=(const BigInt& oth)const{return !(*this<oth);}
    bool operator!=(const BigInt& oth)const{return oth<*this || *this<oth;}
    bool operator==(const BigInt& oth)const{return !(oth<*this) && !(*this<oth);}

    BigInt operator=(const char* num)
    {
        memset(d,0,sizeof(d));
        len=strlen(num);
        for(int i=0;i<len;i++) d[i]=num[len-1-i]-'0';
        clean();
        return *this;
    }
    BigInt operator=(int num)
    {
        char s[20];
        sprintf(s,"%d",num);
        return *this=s;
    }
    BigInt operator+(const BigInt& oth)const
    {
        BigInt c;
        c.len=max(len,oth.len);
        for(int i=0;i<=c.len;i++) c.d[i]=0;
        for(int i=0;i<c.len;i++)
        {
            c.d[i]+=(i<len?d[i]:0)+(i<oth.len?oth.d[i]:0);
            c.d[i+1]+=c.d[i]/10;
            c.d[i]%=10;
        }
        c.len+=(c.d[c.len]>0);
        c.clean();
        return c;
    }
    BigInt operator-(const BigInt& oth)const
    {
        BigInt c=*this;
        if(c<oth) printf("Produce negative number!\n");
        int i;
        for(i=0;i<oth.len;i++)
        {
            c.d[i]-=oth.d[i];
            if(c.d[i]<0) c.d[i]+=10, c.d[i+1]--;
        }
        while(c.d[i]<0) c.d[i++]+=10, c.d[i]--;
        c.clean();
        return c;
    }
    BigInt operator*(const BigInt& oth)const
    {
        BigInt c;
        for(int i=0;i<len;i++) for(int j=0;j<oth.len;j++) c.d[i+j]+=d[i]*oth.d[j];
        for(int i=0;i<len+oth.len || !c.d[i];c.len=++i) c.d[i+1]+=c.d[i]/10, c.d[i]%=10;
        c.clean();
        return c;
    }
    BigInt operator/(const BigInt& oth)const
    {
        BigInt c=*this, r=0;
        for(int i=0;i<len;i++)
        {
            r=r*10+c.d[len-1-i];
            int j;
            for(j=0;j<10;j++) if(r<oth*(j+1)) break;
            c.d[len-1-i]=j;
            r=r-oth*j;
        }
        c.clean();
        return c;
    }
    BigInt operator%(const BigInt& oth)
    {
        BigInt r=0;
        for(int i=0;i<len;i++)
        {
            r=r*10+d[len-1-i];
            int j;
            for(j=0;j<10;j++) if(r<oth*(j+1)) break;
            r=r-oth*j;
        }
        return r;
    }
    BigInt operator+=(const BigInt& oth)
    {
        *this=*this+oth;
        return *this;
    }
    BigInt operator*=(const BigInt& oth)
    {
        *this=*this*oth;
        return *this;
    }
    BigInt operator-=(const BigInt& oth)
    {
        *this=*this-oth;
        return *this;
    }
    BigInt operator/=(const BigInt& oth)
    {
        *this=*this/oth;
        return *this;
    }
};
istream& operator>>(istream& in, BigInt& x)
{
    string s;
    in>>s;
    x=s.c_str();
    return in;
}
ostream& operator<<(ostream& out,const BigInt& x)
{
    out<<x.str();
    return out;
}

int main()
{
    BigInt a,b;
    while(cin>>a>>b)
    {
        cout<<a+b<<" ";
        if(a<b) cout<<'-'<<b-a<<" ";
        else cout<<a-b<<" ";
        cout<<a*b<<" ";
        cout<<a/b<<" ";
        cout<<a%b<<endl;
    }
}

 

posted @ 2018-10-21 15:08  Dilthey  阅读(262)  评论(0编辑  收藏  举报