求大佬

P1001 A+B Problem

ToryChef @ 2023-07-21 14:23:04

这道题怎么做啊,急


by Xuyichao_ @ 2023-07-23 10:18:55

#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 ToryChef @ 2023-07-24 10:09:11

@Seal_ZhouMoTong 你是唯一一个正常的


by SealMoBurp @ 2023-07-24 10:12:30

@miaomimmaomi 谢谢,我这个蒟蒻只会这么做(悲


by zhaiqiwei_2013 @ 2023-07-24 15:19:13

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    cout<<a+b<<endl;
    return 0;
}

by furina_yyds @ 2023-07-25 12:32:49

这道题肥肠的难,你可以用动态规划,搜索,字符串,数学,图论,计算几何,树形数据结构,博弈论等方法。


by BJ_BSGF_Lyc @ 2023-08-01 16:39:30

@miaomimmaomi @miaomimmaomi 你可以用#inculde<iostream>和#include<cstdio>做这个题,别听别人的误导,其实这就是一个入门级的题,代码如下,仅供参考。 include<iostream> include<cstdio> using namespace std; int main()//主函数(非常重要) { int a,b;//定义两个变量(就是输入的那两个数字) cin>>a>>b;//输入两个数字 cout<<a+b;//输出结果 return 0;//结束 } //这样就可以了=>


by coool @ 2023-08-06 13:03:04

@Seal_ZhouMoTong 你舅惯着他爸


by Pink_Cut_Tree @ 2023-08-07 10:44:45

@miaomimmaomi 最短路(雾


by soul2024 @ 2023-08-07 22:05:32

#include<iostream>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}

by qiaozifan @ 2023-08-30 16:13:22

@Xuyichao_ 不必要搞成这样


上一页 | 下一页