zoukankan      html  css  js  c++  java
  • 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!
    ");
            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;
        }
    }
  • 相关阅读:
    Window安装Redis并设置为开机启动
    大佬为你揭秘微信支付的系统架构,你想知道的都在这里了
    想要设计自己的微服务?看这篇文章就对了
    破局人工智能:构建AI,与腾讯云一起探索语音应用场景
    巧用机器学习定位云服务器故障
    重磅发布 | 黑镜调查:深渊背后的真相之「DDoS 威胁与黑灰产业调查报告」
    5分钟内看懂机器学习和深度学习的区别
    如何防范和应对Redis勒索,腾讯云教你出招
    Redis勒索事件爆发,如何避免从删库到跑路?
    作为一个编程新手,我再也不怕Flink迷了我的眼!
  • 原文地址:https://www.cnblogs.com/dilthey/p/9824826.html
Copyright © 2011-2022 走看看