C++ 大数类

#include<iostream>
#include<vector>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
string to_string(long long x)
{
    string ans1,ans2;
    while(x)
    {
        int tt=x%10;
        ans1+=(tt+'0');
        x/=10;
    }
    for(int i=ans1.length()-1;i>=0;i--)
        ans2+=ans1[i];
    return ans2;
}
struct BigInteger
{
    static const int BASE=(int)1e8;
    static const int WIDTH=8;
    vector<int>s;
    BigInteger(long long num=0){*this=num;}
    BigInteger(const string &str){*this=str;}
    BigInteger operator =(long long num)
    {
        s.clear();
        do
        {
            s.push_back(num%BASE);
            num/=BASE;
        }while(num>0);
        return *this;
    }
    BigInteger operator = (const string &str)
    {
        s.clear();
        int x,len=(str.length()-1)/WIDTH+1;
        for(int i=0;i<len;i++)
        {
            int end=str.length()-i*WIDTH;
            int start=max(0,end-WIDTH);
            sscanf(str.substr(start,end-start).c_str(),"%d",&x);
            s.push_back(x);
        }
        return *this;
    }
    BigInteger operator + (const BigInteger &b)const
    {
        BigInteger c;
        c.s.clear();
        for(int i=0,g=0;;i++)
        {
            if(g==0&&i>=s.size()&&i>=b.s.size())break;
            int x=g;
            if(i<s.size())x+=s[i];
            if(i<b.s.size())x+=b.s[i];
            c.s.push_back(x%BASE);
            g=x/BASE;
        }
        return c;
    }
    BigInteger operator *(BigInteger const& b) const
    {
        BigInteger answer;
        for(int i=0;i<s.size();i++)
        {
            long long tmp;
            for(int j=0;j<b.s.size();j++)
            {
                tmp=1LL*s[i]*b.s[j];
                string t=to_string(tmp);
                for(int x=(i+j);x!=0;x--)
                {
                    string s="00000000";
                    t+=s;
                }
                answer=answer+BigInteger(t);
            }
        }
        return answer;
    }
};
ostream& operator << (ostream &out,const BigInteger& x)
{
    out<<x.s.back();
    for(int i=x.s.size()-2;i>=0;i--)
    {
        char buf[20];
        sprintf(buf,"%08d",x.s[i]);
        for(int j=0;j<strlen(buf);j++)
            out<<buf[j];
    }
    return out;
}
istream& operator >>(istream &in,BigInteger& x)
{
    string t;
    in>>t;
    x=t;
    return in;
}
map<int,BigInteger>T;
int main()
{
    //int fff=12345;
    //cout<<to_string(fff)<<endl;
    BigInteger x;
    x=1;
    for(int i=0;i<=1000;i++)
    {
        T[i]=x;
        x=x*2;
    }
    int t;cin>>t;
    while(t--)
    {
        int n;cin>>n;
        cout<<T[n]<<endl;
    }
}
 

以下是一个简单的 C++ 大数实现,它可以实现大数的加减运算: ``` #include <iostream> #include <string> #include <algorithm> using namespace std; class BigInt { public: string num; BigInt() {} BigInt(string s) { num = s; } BigInt operator+(BigInt b) { BigInt res; int carry = 0; int len1 = num.length(), len2 = b.num.length(); int len = max(len1, len2); for (int i = 0; i < len; i++) { int a = i < len1 ? num[len1 - 1 - i] - '0' : 0; int b = i < len2 ? b.num[len2 - 1 - i] - '0' : 0; int sum = a + b + carry; carry = sum / 10; sum = sum % 10; res.num.insert(0, to_string(sum)); } if (carry) { res.num.insert(0, to_string(carry)); } return res; } BigInt operator-(BigInt b) { BigInt res; int borrow = 0; int len1 = num.length(), len2 = b.num.length(); int len = max(len1, len2); for (int i = 0; i < len; i++) { int a = i < len1 ? num[len1 - 1 - i] - '0' : 0; int b = i < len2 ? b.num[len2 - 1 - i] - '0' : 0; int diff = a - b - borrow; if (diff < 0) { diff += 10; borrow = 1; } else { borrow = 0; } res.num.insert(0, to_string(diff)); } while (res.num.length() > 1 && res.num[0] == '0') { res.num.erase(0, 1); } return res; } void print() { cout << num << endl; } }; int main() { BigInt a("123456789"), b("987654321"); BigInt c = a + b; BigInt d = b - a; c.print(); // 输出 1111111110 d.print(); // 输出 864197532 return 0; } ``` 以上代码实现了 BigInt ,这个包含了 num 字符串,用来存储大数。它的构造函数可以接受一个字符串作为参数,用于初始化大数中的 `operator+` 和 `operator-` 分别实现了大数的加法和减法。其中,加法的实现比较简单,使用了竖式加法的思想,从低位开始逐位相加,最后将结果逆序输出即可。减法的实现稍微复杂一些,需要考虑借位的情况。 最后,我们在 `main` 函数中创建了两个大数 a 和 b,分别进行了加法和减法运算,并将结果打印出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值