zoukankan      html  css  js  c++  java
  • C++大数模板

    #include <iostream>
    #include <algorithm>
    #include <iomanip>
    #include <string.h>
    using namespace std;
    const int MAXN=10005;
    const int BASE=100000;
    const int LEN=5;//防止int溢出,最多基数最多设为5位
    int max(int a,int b)
    {
        if(a>b)
            return a;
        return b;
    }
    struct BigInt{
    public:
        int e[MAXN];
        int len;
        BigInt()
        {
            memset(e,0,sizeof(e));
            len=0;
        }
        void set(int x)
        {
            while(x>0)
            {
                e[len++]=x%BASE;
                x/=BASE;
            }
        }
        bool operator>(const BigInt &b)
        {
            if(len>b.len)
            {
                return true;
            }
            else if(len==b.len)
            {
                int i;
                for(i=len-1;i>=0;i--)
                {
                    if(e[i]>b.e[i])    return true;
                    else if(e[i]<b.e[i])    return false;
                    else ;
                }
                return false;
            }
            else    
            {
                return false;
            }
        }
        BigInt operator+(const BigInt &b)
        {
            BigInt res;
            res.len=max(len,b.len);
            int up=0;
            for(int i=0;i<res.len;i++)
            {
                int z=e[i]+b.e[i];
                res.e[i]=z%BASE;
                up=z/BASE;
            }
            if(up!=0)    res.e[res.len++]=up;
            return res;
        }
        BigInt operator-(const BigInt &b)
        {
            BigInt res;
            res.len=max(len,b.len);
            int down=0;
            for(int i=0;i<res.len;i++)
            {
                int z=e[i]-b.e[i]-down;
                if(z<0)
                {
                    z+=BASE;
                    down=1;
                }
                else
                {
                    down=0;
                }
                res.e[i]=z;
            }
            while(res.len>1&&res.e[res.len-1]==0)    res.len--;
            return res;
        }
        BigInt operator*(const BigInt &b)
        {
            BigInt res;
            for(int i=0;i<len;i++)
            {
                int up=0;
                for(int j=0;j<b.len;j++)
                {
                    int z=(e[i]*b.e[j]+res.e[i+j]+up);
                    res.e[i+j]=z%BASE;
                    up=z/BASE;
                }
                if(up!=0)    res.e[i+b.len]=up;
            }
            res.len=len+b.len;
            while(res.len>1&&res.e[res.len-1]==0)    res.len--;
            return res;
        }
        BigInt operator/(const int &b)
        {
            BigInt res=*this;
            int carry=0;
            for(int i=len-1;i>=0;i--)
            {
                res.e[i]+=carry*BASE;
                carry=res.e[i]%b;
                res.e[i]/=b;
            }
            while(res.len>1&&res.e[res.len-1]==0)    res.len--;
            return res;
        }
        BigInt operator%(const int &b)
        {
            BigInt tmp=*this;
            BigInt B;
            B.set(b);
            BigInt res=tmp-(tmp/b)*B;
            return res;
        }
        friend ostream &operator<<(ostream &out,const BigInt &res)
        {
            out<<res.e[res.len-1];
            for(int i=res.len-2;i>=0;i--)
            {
                out<<setw(LEN)<<setfill('0')<<res.e[i];
            }
            out<<endl;
            return out;
        }
    };
    int main()
    {
        BigInt res;
        res.set(1092);
        res=res%1091;
        cout<<res;
        return 0;
    }
  • 相关阅读:
    Oracle SQL语句收集
    SqlParameter In 查询
    SQL 性能优化
    Entity Framework
    【XLL API 函数】 xlfSetName
    【XLL API 函数】xlfUnregister (Form 2)
    【XLL API 函数】xlfUnregister (Form 1)
    【Excel 4.0 函数】REGISTER 的两种形式以及VBA等效语句
    【Excel 4.0 函数】REGISTER
    【Bochs 官方手册翻译】 第一章 Bochs介绍
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5674709.html
Copyright © 2011-2022 走看看