求大佬解答

P1001 A+B Problem

mobei @ 2023-07-19 22:43:10

∣a∣,∣b∣≤10 9 本题的这个条件用不用管


by yzm0325 @ 2023-07-19 22:48:08

@mobei 当然用,如果你用高精度的话


by mobei @ 2023-07-19 23:22:22

@zym0325 高精度的话是什么,我是新手,不太懂,求解答


by yzm0325 @ 2023-07-19 23:35:26

@mobei 就是当数据范围大盗 到 longlong 也装不下的时候,就要用数组来模拟加法竖式,就是高精度。


by wangchuanle @ 2023-07-22 18:29:38

@mobei 这个就是高精加模板

#include<bits/stdc++.h>
using namespace std;
int lenc=1,x,a1[10005],b1[10005],c1[10005];
char s,a[10005],b[10005];
int main()
{
    scanf("%s%s",&a,&b);
    int lena=strlen(a);
    int lenb=strlen(b);
    for(int i=0;i<lena;i++)a1[lena-i]=a[i]-'0';
    for(int i=0;i<lenb;i++)b1[lenb-i]=b[i]-'0';
    while(lenc<=lena||lenc<=lenb)
    {
        c1[lenc]=a1[lenc]+b1[lenc]+x;
        x=c1[lenc]/10;
        c1[lenc]%=10;
        lenc++;
    }
    c1[lenc]=x;
    while(c1[lenc]==0&&lenc>1)lenc--;
    for(int i=lenc;i>=1;i--)
      cout<<c1[i];
    return 0;
}

当然了,这个代码过不了P1001,因为题目数据里会有负数


by Xuyichao_ @ 2023-07-23 11:20:12

能过的高精度(虽然没必要写这么多)

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
using namespace std;

class bigint{
    private:
        vector<short>num;
    public:
        bool op;
        size_t size()const{return num.size();}
        bigint();
        bigint(const string);
        bigint(const char[]);
        bigint(const unsigned long long);
        bigint(const long long);
        bigint(const long);
        bigint(const int);
        bigint(const short);

        operator long long()const;
        operator long()const;
        operator int()const;
        operator short()const;
        operator unsigned long long()const; 

        friend istream& operator>>(istream&,bigint&);
        friend ostream& operator<<(ostream&,const bigint&);

        friend bool operator>(const bigint&,const bigint&);
        friend bool operator<(const bigint&,const bigint&);
        friend bool operator==(const bigint&,const bigint&);
        friend bool operator!=(const bigint &a,const bigint &b){return !(a==b);}
        friend bool operator>=(const bigint &a,const bigint &b){return !(a<b);}
        friend bool operator<=(const bigint &a,const bigint &b){return !(a>b);}

        friend bigint operator-(const bigint &a){bigint t=a;t.op=!t.op;return t;}
        friend bigint operator+(const bigint&,const bigint&);
        friend bigint operator-(const bigint &a,const bigint &b){return a+(-b);}
        friend bigint operator*(const bigint&,const bigint&);
        friend bigint operator/(const bigint&,const bigint&);
        friend bigint operator%(const bigint &a,const bigint &b){return a-a/b*b;}

        friend bigint operator+(const bigint &a,const unsigned long long &b) {return a+(bigint)b;}
        friend bigint operator+(const bigint &a,const long long &b)          {return a+(bigint)b;}
        friend bigint operator+(const bigint &a,const unsigned long &b)      {return a+(bigint)(long long)b;}
        friend bigint operator+(const bigint &a,const long &b)               {return a+(bigint)b;}
        friend bigint operator+(const bigint &a,const unsigned int &b)       {return a+(bigint)(long long)b;}
        friend bigint operator+(const bigint &a,const int &b)                {return a+(bigint)b;}
        friend bigint operator+(const bigint &a,const unsigned short &b)     {return a+(bigint)(long long)b;}
        friend bigint operator+(const bigint &a,const short &b)              {return a+(bigint)b;}
        friend bigint operator+(const unsigned long long &a,const bigint &b) {return b+a;}
        friend bigint operator+(const long long &a,const bigint &b)          {return b+a;}
        friend bigint operator+(const unsigned long &a,const bigint &b)      {return b+a;}
        friend bigint operator+(const long &a,const bigint &b)               {return b+a;}
        friend bigint operator+(const unsigned int &a,const bigint &b)       {return b+a;}
        friend bigint operator+(const int &a,const bigint &b)                {return b+a;}
        friend bigint operator+(const unsigned short &a,const bigint &b)     {return b+a;}
        friend bigint operator+(const short &a,const bigint &b)              {return b+a;}

        friend bigint operator-(const bigint &a,const unsigned long long &b) {return a-(bigint)b;}
        friend bigint operator-(const bigint &a,const long long &b)          {return a-(bigint)b;}
        friend bigint operator-(const bigint &a,const unsigned long &b)      {return a-(bigint)(long long)b;}
        friend bigint operator-(const bigint &a,const long &b)               {return a-(bigint)b;}
        friend bigint operator-(const bigint &a,const unsigned int &b)       {return a-(bigint)(long long)b;}
        friend bigint operator-(const bigint &a,const int &b)                {return a-(bigint)b;}
        friend bigint operator-(const bigint &a,const unsigned short &b)     {return a-(bigint)(long long)b;}
        friend bigint operator-(const bigint &a,const short &b)              {return a-(bigint)b;}
        friend bigint operator-(const unsigned long long &a,const bigint &b) {return b-a;}
        friend bigint operator-(const long long &a,const bigint &b)          {return b-a;}
        friend bigint operator-(const unsigned long &a,const bigint &b)      {return b-a;}
        friend bigint operator-(const long &a,const bigint &b)               {return b-a;}
        friend bigint operator-(const unsigned int &a,const bigint &b)       {return b-a;}
        friend bigint operator-(const int &a,const bigint &b)                {return b-a;}
        friend bigint operator-(const unsigned short &a,const bigint &b)     {return b-a;}
        friend bigint operator-(const short &a,const bigint &b)              {return b-a;}

        friend bigint operator*(const bigint &a,const unsigned long long &b) {return a*(bigint)b;}
        friend bigint operator*(const bigint &a,const long long &b)          {return a*(bigint)b;}
        friend bigint operator*(const bigint &a,const unsigned long &b)      {return a*(bigint)(long long)b;}
        friend bigint operator*(const bigint &a,const long &b)               {return a*(bigint)b;}
        friend bigint operator*(const bigint &a,const unsigned int &b)       {return a*(bigint)(long long)b;}
        friend bigint operator*(const bigint &a,const int &b)                {return a*(bigint)b;}
        friend bigint operator*(const bigint &a,const unsigned short &b)     {return a*(bigint)(long long)b;}
        friend bigint operator*(const bigint &a,const short &b)              {return a*(bigint)b;}
        friend bigint operator*(const unsigned long long &a,const bigint &b) {return b*a;}
        friend bigint operator*(const long long &a,const bigint &b)          {return b*a;}
        friend bigint operator*(const unsigned long &a,const bigint &b)      {return b*a;}
        friend bigint operator*(const long &a,const bigint &b)               {return b*a;}
        friend bigint operator*(const unsigned int &a,const bigint &b)       {return b*a;}
        friend bigint operator*(const int &a,const bigint &b)                {return b*a;}
        friend bigint operator*(const unsigned short &a,const bigint &b)     {return b*a;}
        friend bigint operator*(const short &a,const bigint &b)              {return b*a;}

        friend bigint operator/(const bigint &a,const unsigned long long &b) {return a/(bigint)b;}
        friend bigint operator/(const bigint &a,const long long &b)          {return a/(bigint)b;}
        friend bigint operator/(const bigint &a,const unsigned long &b)      {return a/(bigint)(long long)b;}
        friend bigint operator/(const bigint &a,const long &b)               {return a/(bigint)b;}
        friend bigint operator/(const bigint &a,const unsigned int &b)       {return a/(bigint)(long long)b;}
        friend bigint operator/(const bigint &a,const int &b)                {return a/(bigint)b;}
        friend bigint operator/(const bigint &a,const unsigned short &b)     {return a/(bigint)(long long)b;}
        friend bigint operator/(const bigint &a,const short &b)              {return a/(bigint)b;}
        friend bigint operator/(const unsigned long long &a,const bigint &b) {return b/a;}
        friend bigint operator/(const long long &a,const bigint &b)          {return b/a;}
        friend bigint operator/(const unsigned long &a,const bigint &b)      {return b/a;}
        friend bigint operator/(const long &a,const bigint &b)               {return b/a;}
        friend bigint operator/(const unsigned int &a,const bigint &b)       {return b/a;}
        friend bigint operator/(const int &a,const bigint &b)                {return b/a;}
        friend bigint operator/(const unsigned short &a,const bigint &b)     {return b/a;}
        friend bigint operator/(const short &a,const bigint &b)              {return b/a;}

        friend bigint operator%(const bigint &a,const unsigned long long &b) {return a%(bigint)b;}
        friend bigint operator%(const bigint &a,const long long &b)          {return a%(bigint)b;}
        friend bigint operator%(const bigint &a,const unsigned long &b)      {return a%(bigint)(long long)b;}
        friend bigint operator%(const bigint &a,const long &b)               {return a%(bigint)b;}
        friend bigint operator%(const bigint &a,const unsigned int &b)       {return a%(bigint)(long long)b;}
        friend bigint operator%(const bigint &a,const int &b)                {return a%(bigint)b;}
        friend bigint operator%(const bigint &a,const unsigned short &b)     {return a%(bigint)(long long)b;}
        friend bigint operator%(const bigint &a,const short &b)              {return a%(bigint)b;}
        friend bigint operator%(const unsigned long long &a,const bigint &b) {return b%a;}
        friend bigint operator%(const long long &a,const bigint &b)          {return b%a;}
        friend bigint operator%(const unsigned long &a,const bigint &b)      {return b%a;}
        friend bigint operator%(const long &a,const bigint &b)               {return b%a;}
        friend bigint operator%(const unsigned int &a,const bigint &b)       {return b%a;}
        friend bigint operator%(const int &a,const bigint &b)                {return b%a;}
        friend bigint operator%(const unsigned short &a,const bigint &b)     {return b%a;}
        friend bigint operator%(const short &a,const bigint &b)              {return b%a;}

        friend bigint operator+=(bigint &a,const bigint &b){a=a+b;return a+b;}
        friend bigint operator-=(bigint &a,const bigint &b){a=a-b;return a-b;}
        friend bigint operator*=(bigint &a,const bigint &b){a=a*b;return a*b;}
        friend bigint operator/=(bigint &a,const bigint &b){a=a/b;return a/b;}
        friend bigint operator%=(bigint &a,const bigint &b){a=a%b;return a%b;}
        friend bigint operator++(bigint &a){a=a+1;return a;}        //++a
        friend bigint operator++(bigint &a,int){a=a+1;return a-1;}  //a++
        friend bigint operator--(bigint &a){a=a-1;return a;}        //--a
        friend bigint operator--(bigint &a,int){a=a-1;return a+1;}  //a--
};

bigint::bigint(){
    op=1;
    num.push_back(0);
}
bigint::bigint(const string a){
    for(size_t i=1;i<a.size();i++)if(a[i]>'9'||a[i]<'0')return;
    if(!(a[0]=='-'||('0'<=a[0]&&a[0]<='9')))return;
    size_t i=0;
    if(a[0]=='-'){op=0;i=1;}else op=1;
    for(;i<a.size();i++)num.push_back(a[i]-'0');
}
bigint::bigint(const char a[]){
    for(size_t i=1;i<strlen(a);i++)if(a[i]>'9'||a[i]<'0')return;
    if(!(a[0]=='-'||('0'<=a[0]&&a[0]<='9')))return;
    size_t i=0;
    if(a[0]=='-'){op=0;i=1;}else op=1;
    for(;i<strlen(a);i++)num.push_back(a[i]-'0');
}
bigint::bigint(const unsigned long long a){
    op=1;
    auto _a=a;
    while(_a){
        num.push_back(_a%10);
        _a/=10;
    }
    for(int i=0,j=num.size()-1;i<j;i++,j--){
        short t=num[i];
        num[i]=num[j];
        num[j]=t;
    }
}
bigint::bigint(const long long a){
    auto _a=a;
    if(_a<0){op=0;_a=-_a;}else op=1;
    while(_a){
        num.push_back(_a%10);
        _a/=10;
    }
    for(int i=0,j=num.size()-1;i<j;i++,j--){
        short t=num[i];
        num[i]=num[j];
        num[j]=t;
    }
}
bigint::bigint(const long a){
    auto _a=a;
    if(_a<0){op=0;_a=-_a;}else op=1;
    while(_a){
        num.push_back(_a%10);
        _a/=10;
    }
    for(int i=0,j=num.size()-1;i<j;i++,j--){
        short t=num[i];
        num[i]=num[j];
        num[j]=t;
    }
}
bigint::bigint(const int a){
    auto _a=a;
    if(_a<0){op=0;_a=-_a;}else op=1;
    while(_a){
        num.push_back(_a%10);
        _a/=10;
    }
    for(int i=0,j=num.size()-1;i<j;i++,j--){
        short t=num[i];
        num[i]=num[j];
        num[j]=t;
    }
}
bigint::bigint(const short a){
    auto _a=a;
    if(_a<0){op=0;_a=-_a;}else op=1;
    while(_a){
        num.push_back(_a%10);
        _a/=10;
    }
    for(int i=0,j=num.size()-1;i<j;i++,j--){
        short t=num[i];
        num[i]=num[j];
        num[j]=t;
    }
}

bigint::operator long long()const{
    long long t=0;
    unsigned long long p=1;
    for(long long i=num.size()-1;i>=0;i--,p*=10)t+=num[i]*p;
    return op==1?t:-t;
}
bigint::operator long()const{
    long t=0;
    unsigned long long p=1;
    for(long long i=num.size()-1;i>=0;i--,p*=10)t+=num[i]*p;
    return op==1?t:-t;
}
bigint::operator int()const{
    int t=0;
    unsigned long long p=1;
    for(long long i=num.size()-1;i>=0;i--,p*=10)t+=num[i]*p;
    return op==1?t:-t;
}
bigint::operator short()const{
    short t=0;
    unsigned long long p=1;
    for(long long i=num.size()-1;i>=0;i--,p*=10)t+=num[i]*p;
    return op==1?t:-t;
}
bigint::operator unsigned long long()const{
    if(op==0)return 0;
    unsigned long long t=0,p=1;
    for(long long i=num.size()-1;i>=0;i--,p*=10)t+=num[i]*p;
    return t;
}

istream& operator>>(istream &is,bigint &a){
    string s;
    is>>s;
    a=s;
    return is;
}
ostream& operator<<(ostream &os,const bigint &a){
    if(a.op==0)os<<'-';
    for(size_t i=0;i<a.num.size();i++){
        os<<a.num[i];
    }
    return os;
}

bool operator>(const bigint &a,const bigint &b){
    if(a.op!=b.op)return a.op>b.op;
    if(a.op==0&&b.op==0)return !(a>b);
    if(a.size()!=b.size())return a.size()>b.size();
    for(size_t i=0;i<a.size();i++){
        if(a.num[i]!=b.num[i]){
            return a.num[i]>b.num[i];
        }
    }
    return false;
}
bool operator<(const bigint &a,const bigint &b){
    if(a.op!=b.op)return a.op<b.op;
    if(a.op==0&&b.op==0)return !(a<b);
    if(a.size()!=b.size())return a.size()<b.size();
    for(size_t i=0;i<a.size();i++){
        if(a.num[i]!=b.num[i]){
            return a.num[i]<b.num[i];
        }
    }
    return false;
}
bool operator==(const bigint &a,const bigint &b){
    if(a.op!=b.op)return false;
    if(a.size()!=b.size())return false;
    for(size_t i=0;i<a.size();i++){
        if(a.num[i]!=b.num[i]){
            return false;
        }
    }
    return true;
}
bigint operator+(const bigint &a,const bigint &b){
    bigint t=a;
    if(a.op==1&&b.op==1){
        if(a<b)return b+a;
        for(long long ai=a.size()-1,bi=b.size()-1;bi>=0;ai--,bi--)t.num[ai]+=b.num[bi];
        for(size_t i=t.size()-1;i>0;i--)if(t.num[i]>9){
            t.num[i]-=10;
            t.num[i-1]++;
        }
        if(t.num[0]>9){
            t.num[0]-=10;
            t.num.insert(t.num.begin(),1);
        }
    }else if(a.op==1&&b.op==0){
        if(a<-b)return -((-b)+(-a));
        for(long long ai=a.size()-1,bi=b.size()-1;bi>=0;ai--,bi--)t.num[ai]-=b.num[bi];
        for(size_t i=t.size();i>0;i--)if(t.num[i]<0){
            t.num[i]+=10;
            t.num[i-1]--;
        }
        if(t.num[0]==0&&t.size()!=1)t.num.erase(t.num.begin());
    }else if(a.op==0&&b.op==1){
        return b+a;
    }else if(a.op==0&&b.op==0){
        return -((-a)+(-b));
    }
    return t;
}
bigint operator*(const bigint &a,const bigint &b){
    if(a.op==0&&b.op==1)return -(-a)*b;
    if(a.op==1&&b.op==0)return -a*(-b);
    if(a.op==0&&b.op==0)return (-b)*(-a);
    bigint t;
    for(bigint i;i<b;i=i+1)t=t+a;
    return t;
}
bigint operator/(const bigint &a,const bigint &b){
    if(a.op==0&&b.op==1)return -((-a)/b);
    if(a.op==1&&b.op==0)return -(a/(-b));
    if(a.op==0&&b.op==0)return (-b)/(-a);
    bigint t=a,cnt;
    while(t>=(bigint)0){
        t=t-b;
        cnt=cnt+1;
    }
    return cnt-1;
}

int main(){
    bigint a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

by MrPython @ 2023-07-30 09:51:47

@Xuyichao_ 或许没必要枚举所有的整数类型()

构造函数写好C++可以自动进行类型转换


by peijunshuo_9961 @ 2023-08-01 16:38:10

xiao puo ti~~~~


by SF_bee @ 2023-08-05 14:48:32

@Xuyichao_ 到时候你改代码也得用大脑高精度改了。


by 3_1415926 @ 2023-08-10 15:29:40

要高精度吗???????????????


by OldSix_scc @ 2023-08-28 20:13:53

这个题不用那么麻烦,直接用int就行了。


| 下一页