c++ BigInteger模板 非常好的板子

转一手非常好的BigInteger板子,虽然我也没太看懂,但经过无脑套用,还是很好用的。

#include <cstdio>  
#include <algorithm>  
#include <cstring>  
using namespace std;  
  
struct BigInteger{  
    int A[25];//这里更改数组长度可以更改大整数的位数,很强大  
    enum{MOD = 10000};  
    BigInteger(){memset(A, 0, sizeof(A)); A[0]=1;}  
    void set(int x){memset(A, 0, sizeof(A)); A[0]=1; A[1]=x;}  
    void print(){  
        printf("%d", A[A[0]]);  
        for (int i=A[0]-1; i>0; i--){  
            if (A[i]==0){printf("0000"); continue;}  
            for (int k=10; k*A[i]<MOD; k*=10) printf("0");  
            printf("%d", A[i]);  
        }  
        printf("\n");  
    }  
    int& operator [] (int p) {return A[p];}  
    const int& operator [] (int p) const {return A[p];}  
    BigInteger operator + (const BigInteger& B){  
        BigInteger C;  
        C[0]=max(A[0], B[0]);  
        for (int i=1; i<=C[0]; i++)  
            C[i]+=A[i]+B[i], C[i+1]+=C[i]/MOD, C[i]%=MOD;  
        if (C[C[0]+1] > 0) C[0]++;  
        return C;  
    }  
    BigInteger operator * (const BigInteger& B){  
        BigInteger C;  
        C[0]=A[0]+B[0];  
        for (int i=1; i<=A[0]; i++)  
            for (int j=1; j<=B[0]; j++){  
                C[i+j-1]+=A[i]*B[j], C[i+j]+=C[i+j-1]/MOD, C[i+j-1]%=MOD;  
            }  
        if (C[C[0]] == 0) C[0]--;  
        return C;  
    }  
};  
int main() {  
    BigInteger a, b;  
    a.set(1); b.set(1);  
    (a+b).print();  
  
    return 0;  
}  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值