• bignum 大数模板


      今天无意间看到一个很好的大数模板,能算加、减、乘、除等基本运算,但操作减法的时候只能大数减小数,也不支持负数,如果是两个负数的话去掉符号相加之后再取反就可以了,一正一负比较绝对值大小,然后相减。我借用了一下:(作过少许代码上的精简)

    #include<cstdio> 
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    const int maxn = 2003;
    
    struct bign{
        int len, s[maxn];
        bign(){
            memset(s,0,sizeof(s));
            len= 1;
        }
        bign(int num)    {    *this = num;    }
        bign(const char *num)    {    *this = num;    }
        bign operator = (const int num){
            char s[maxn];
            sprintf(s,"%d",num);
            return *this = s;
        }
        bign operator = (const char *num){
            for(int i = 0; num[i] && num[i] =='0'; ++num) ;
            len = strlen(num);
            for(int i = 0; i < len; ++i)
                s[i] = num[len-1-i] -'0';
            return *this;
        }
        bign operator + (const bign &b) const {
            bign c;
            c.len = 0;
            int maxlen = max(len, b.len);
            for(int i = 0, g = 0; g || i < maxlen; ++i){    // g是进位 
                int x = g;
                if(i < len)      x += s[i];
                if(i < b.len)    x += b.s[i];
                c.s[c.len++] = x % 10;
                g = x / 10;
            }
            return c;
        }
        bign operator += (const bign &b){
            return *this = *this + b;
        }
        void clean(){
            while(len >1 && !s[len-1])    --len;
        }
        bign operator * (const bign &b) const {
            bign c;
            c.len = len + b.len;
            
            for(int i = 0; i < len; ++i)
                for(int j = 0; j < b.len; ++j)
                    c.s[i+j] += s[i] * b.s[j];
                    
            for(int i = 0; i < c.len; ++i){
                c.s[i+1] += c.s[i]/10;
                c.s[i] %= 10;
            }
            c.clean();
            return c;
        }
        bign operator *= (const bign &b){
            return *this = *this * b;
        }
        bign operator -(const bign &b) const {
            bign c;
            c.len = 0;
            for(int i = 0, g = 0; i < len; ++i){    //此时的 g是退位
                int x = s[i]- g;
                if(i < b.len)    x -= b.s[i];
                if(x >= 0)    g = 0;
                else {
                    g = 1;
                    x += 10;
                }
                c.s[c.len++] = x;
            }
            c.clean();
            return c;
        }
        bign operator -= (const bign &b){
            return *this = *this - b;
        }
        bign operator / (const bign &b) const {
            bign c, f = 0;
            for(int i = len-1; i >= 0; --i){
                f *= 10;
                f.s[0] = s[i];
                while(f >= b){
                    f -= b;
                    ++c.s[i];
                }
            }
            c.len = len;
            c.clean();
            return c;
        }
        bign operator /= (const bign &b){
            return *this = *this / b;
        }
        bign operator % (const bign &b) const {
            bign r = *this / b;
            return *this- r*b;
        }
        bign operator %= (const bign &b){
            return *this = *this % b;
        }
        bool operator < (const bign &b) const {
            if(len != b.len)    return len < b.len;
            for(int i= len-1; i>=0; --i)
                if(s[i] != b.s[i])    return s[i] < b.s[i];
            return 0;
        }
        bool operator > (const bign &b) const {
            if(len != b.len)    return len > b.len;
            for(int i = len-1; i >= 0; --i)
                if(s[i] != b.s[i])    return s[i] > b.s[i];
            return 0;
        }
        bool operator == (const bign &b) const {
            return !(*this < b || *this > b);
        }
        bool operator != (const bign &b) const {
            return !(*this == b);
        }
        bool operator <= (const bign &b) const {
            return *this < b || *this == b;
        }
        bool operator >= (const bign &b) const {
            return *this > b || *this == b;
        }
        string str() const {
            string res = "";
            for(int i = 0; i < len; ++i)
                res = char(s[i]+'0') + res;        //这里一定不能掉乱 +的顺序! 
            return res;
        }
    };
    
    istream& operator >> (istream &in, bign &x){
        string s;
        in >> s;
        x = s.c_str();
        return in;
    }
    
    ostream& operator << (ostream &out, const bign &x){
        out << x.str();
        return out;
    }
    
    int main()
    {
        bign a,b,c,d,e,f,g;
        while(cin>>a>>b)
        {
            a.clean();
            b.clean();
            c = a+b;
            d = a-b;
            e = a*b;
            f = a/b;
            g = a%b;
            cout<<"a+b = "<<c<<endl;
            cout<<"a-b = "<<d<<endl;
            cout<<"a*b = "<<e<<endl;
            cout<<"a/b = "<<f<<endl;
            cout<<"a%b = "<<g<<endl;
            cout<<(a==b? "a = b":"a != b")<<endl;
        }
        return 0;
    }

      在此感谢原博客的模板提供: bign类C++高精度模板

  • 相关阅读:
    [BZOJ]2589: Spoj 10707 Count on a tree II
    [BZOJ]2434: [Noi2011]阿狸的打字机
    Codeforces Round #408 (Div. 2)
    [BZOJ]2653: middle
    洛谷4月月赛R1
    2017省夏令营Day8
    2017省夏令营Day7
    2017省夏令营Day6
    【20170604校内模拟赛】香蕉
    【20170602模拟赛】秋之国的夏日祭
  • 原文地址:https://www.cnblogs.com/Newdawn/p/4248365.html
Copyright © 2020-2023  润新知