高精度重载:+ /

通用板子

  • 简易code

#include<stdio.h> 
#include<string.h>
#include<algorithm> 
using namespace std;
const int mxn=1e4+10;
struct BigInt {
    int num[mxn],len;
    void reset() {
        memset(num,0,sizeof(num));len=0;
    }
    BigInt operator+ (const BigInt &x) {
        BigInt c;
        c.reset();
        int t=0;c.len=max(len,x.len); 
        for(int i=1;i<=c.len;++i) {
            c.num[i]=num[i]+x.num[i]+t;
            t=c.num[i]/10;
            c.num[i]%=10;
        }
        if(t) c.num[++c.len]=t;
        return c;
    }
    BigInt operator- (const BigInt &x) {
        BigInt c;
        c.reset();
        c.len=max(x.len,len);
        for(int i=1;i<=c.len;++i) {
            c.num[i]+=(num[i]-x.num[i]);
            if(c.num[i]<0) {
                c.num[i]+=10;
                c.num[i+1]--;
            }
        }
        while(c.num[c.len]==0) c.len--; 
        return c;
    } 
    BigInt operator* (const BigInt &x) {
        BigInt c;
        c.reset();
        for(int i=1;i<=len;++i) {
            for(int j=1;j<=x.len;++j) {
                c.num[i+j-1]+=num[i]*x.num[j];
                c.num[i+j]+=c.num[i+j-1]/10; 
                c.num[i+j-1]%=10;
            }
        }
        c.len=len+x.len;
        while(!c.num[c.len] && c.len>1) c.len--;
        return c;
    }
    
    bool operator<(const BigInt &x) {
        if(len!=x.len) return len<x.len;
        for(int i=len;i>=1;--i) if(num[i]!=x.num[i]) return num[i]<x.num[i];  
        return false;
    }
    bool operator==(const BigInt &x) {
        if(len!=x.len) return false;
        for(int i=len;i>=1;--i) if(num[i]!=x.num[i]) return false;
        return true;
    }
    void read() {
        char str[mxn];
        memset(str,0,sizeof(str));
        scanf("%s",str);
        len=strlen(str);
        for(int i=1;i<=len;++i) num[i]=str[len-i]-'0';
    }
    void print() {
        for(int i=len;i;i--) {
            printf("%d",num[i]);
        }
        printf("
");
    }
}a,b,ans;
int cnt;

int main() 
{
    a.read(),b.read();
    ans=a+b;
    ans.print();
    return 0;
}

 

  • 高级code

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct node {
    int l,r,f;
}a[1001];

bool cmp(node a,node b){
    return a.f<b.f;
}

struct BigInt {
    vector<int> v;
    static const int BASE = 10000;
    static const int WIDTH = 4;
    BigInt(long long x) {
        do {
            v.push_back(x % BASE);
        }while (x /= BASE);
    }
    BigInt(string str) {
        int size = str.length();
        v.reserve(size);
        for(int i = size - 1; i >= 0; i-=WIDTH){
            string sub;
            if(i-WIDTH + 1<0)sub = str.substr(0,i+1);
            else sub = str.substr(i - WIDTH +1,WIDTH);
            int temp = atoi(sub.c_str());
            v.push_back(temp);
        }
    }
    BigInt() {
        
    }
    void removePreZero() {
        while(v.size() >= 1 && v.back() == 0) v.pop_back();
    }
    bool operator<(const BigInt &a) const {
        if (v.size() != a.v.size()) {
            return v.size() < a.v.size();
        }
        for (int i = v.size() - 1; i >= 0; i--) {
            if (v[i] != a.v[i]) {
                return v[i] < a.v[i];
            }
        }
        return false;
    }
    bool operator>(const BigInt &a) const {return a < *this;}
    bool operator<=(const BigInt &a) const {return !(a < *this);}
    bool operator>=(const BigInt &a) const {return !(*this < a);}
    bool operator!=(const BigInt &a) const {return a < *this || a > *this;}
    bool operator==(const BigInt &a) const {return !(a < *this) && !(a > *this);}
    BigInt operator+(const BigInt &a) const {
        BigInt ans;
        ans.v.reserve(max(a.v.size(), v.size()));
        int sum = 0;
        for (int i = 0; i < max(a.v.size(), v.size()); i++) {
            if (i < a.v.size()) sum += a.v[i];
            if (i < v.size()) sum += v[i];
            ans.v.push_back(sum % BASE);
            sum /= BASE;
        }
        if (sum) ans.v.push_back(sum);
        ans.removePreZero();
        return ans;
    }
/*    BigInt operator+=(const BigInt &a) const {
        return *this = *this + a;
    }*/
    BigInt operator-(const BigInt &a) const {
        BigInt ans;
        ans.v.reserve(max(a.v.size(), v.size()));
        int dif = 0;
        for (int i = 0; i < max(a.v.size(), v.size()); i++) {
            if (i < v.size()) dif += v[i];
            if (i < a.v.size()) dif -= a.v[i];
            if (dif >= 0) {
                ans.v.push_back(dif);
                dif = 0;
            } else {
                ans.v.push_back((dif +BASE) % BASE);
                dif = -1;
            }
        }
        ans.removePreZero();
        return ans;
    }
/*    BigInt operator-=(const BigInt &a) const {
        return *this = *this - a;
    }*/
    BigInt operator*(const BigInt &a) const {
        BigInt ans;
        ans.v.resize(v.size() + a.v.size(), 0);
        for (int i = 0; i < v.size(); i++) {
            for (int j = 0; j < a.v.size(); j++) {
                ans.v[i + j] += v[i] * a.v[j];
                ans.v[i + j + 1] += ans.v[i + j] / BASE;
                ans.v[i + j] %= BASE;
            }
        }
        ans.removePreZero();
        return ans;
    }
/*    BigInt operator*=(const BigInt &a) const {
        return *this = *this * a;
    }*/ 
    BigInt operator/(const BigInt &a) const {
        BigInt ans, ret(0);
        ans.v.resize(v.size(), 0);
    //    ret = 0;
        for (int i = v.size() - 1; i >= 0; i--) {
            ret = ret * BASE + v[i];
            while (ret >= a) {
                ret = ret - a;
                ans.v[i]++;
            }
        }
        ans.removePreZero();
        return ans;
    }
    BigInt operator/(const int &a) const {
        BigInt ans;
        ans.v.resize(v.size(), 0);
        int    ret = 0;
        for (int i = v.size() - 1; i >= 0; i--) {
            ret += v[i];
            if(ret >= a){
                ans.v[i] += ret/a;
                ret%=a;
            }
            ret*=BASE;
        }
        ans.removePreZero();
        return ans;
    }
    
    void print(){
        if(v.size()==0)printf("0");
        for(int i = v.size() - 1 ; i >= 0 ; i--){
            if(i!=v.size()-1){
                if(v[i]<10)printf("000");
                else if(v[i]<100)printf("00");
                else if(v[i]<1000)printf("0");
            }
            printf("%d",v[i]);
        }
    
    }
/*    BigInt operator/=(const BigInt &a) const {
        return *this = *this / a;
    }*/
}ans,pre,t;
int n,temp,t1,t2;

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i) {
        BigInt t;
        t=1;
        for(int j=2;j<=i;++j) {
            t=t*j;
        }
        ans=ans+t;
    }    
    ans.print();
    return 0;
} 
原文地址:https://www.cnblogs.com/qseer/p/9865306.html