题解 | #a+b#

a+b

https://www.nowcoder.com/practice/4c39c984ea3848b48e111b8e71ec1dd4

#include <iostream>
#include<math.h>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
const int MAX = 10000;

struct BigInteger {
    int digit[MAX];
    int length;
    BigInteger();
    BigInteger(int x);
    BigInteger(string str);
    BigInteger(const BigInteger& b);
    BigInteger operator=(int x);
    BigInteger operator=(string str);
    BigInteger operator=(const BigInteger& b);
    bool operator<=(const BigInteger& b);
    bool operator==(const BigInteger& b);
    BigInteger operator+(const BigInteger& b);
    BigInteger operator-(const BigInteger& b);
    BigInteger operator*(const BigInteger& b);
    BigInteger operator/(const BigInteger& b);
    BigInteger operator%(const BigInteger& b);
    friend istream& operator>>(iostream& in, BigInteger& x);
    friend ostream& operator<<(ostream& out, const BigInteger& x);
};
BigInteger::BigInteger() {
    memset(digit, 0, sizeof(digit));
    length = 0;
}
bool BigInteger::operator<=(const BigInteger& b) {
    int len = length;
    bool tag = false;
    if (b.length > len) len = b.length;

    for (int i = len - 1; i >= 0; i--) {
        if (digit[i] < b.digit[i]) {
            return true;
        } else if (digit[i] > b.digit[i]) {
            return false;
        } else {
            if (i == len - 1) return true;

        }
    }
    return true;
}
//成功
BigInteger BigInteger::operator+(const BigInteger&
                                 b) { //时刻记得这个不是构造器呜呜
    BigInteger res;
    int carry = 0;
    int length = 0;
    int current = 0;
//    cout<<length;
    for (int i = 0; i < b.length; i++) {
        current = digit[i] + b.digit[i] + carry;
        res.digit[res.length++] = current % 10;
        carry = current / 10;
    }
    return res;
}
//成功
BigInteger BigInteger ::operator=(int x) {
    memset(digit, 0, sizeof(digit));
    length = 0;
    if (x == 0) {
        length++;
    } else {
        while (x) {
            digit[length++] = x % 10;
            x /= 10;
        }
    }
    return *this;
}
BigInteger BigInteger ::operator=(const BigInteger& b) {
    memset(digit, 0, sizeof(digit));
    length = b.length;
//    cout<<length;
    for (int i = 0; i < b.length; i++) {
        digit[i] = b.digit[i];
    }
    return* this;
}
//成功
BigInteger ::BigInteger(int x) {
    memset(digit, 0, sizeof(digit));
    length = 0;
    while (x) {
        digit[length++] = x % 10;
        x /= 10;
    }
}
//成功
BigInteger ::BigInteger(const BigInteger& b) { //构造器
    memset(digit, 0, sizeof(digit));
    length = b.length;
    for (int i = 0; i < length; i++) {
        digit[i] = b.digit[i];
    }
}
//成功
ostream& operator<<(ostream& out, const BigInteger& x) { //注意用别名
    for (int i = x.length - 1; i >= 0; i--) {
        out << x.digit[i];
    }
    return out;
}
//成功
BigInteger::BigInteger(string str) {
    memset(digit, 0, sizeof(digit));
    length = str.size();
//    cout<<length;
    for (int i = length - 1; i >= 0; i--) {

//            cout<<"\n"<<str[i]-'0'<<"\n";
        digit[length - i - 1] = str[i] - '0'; //别忘减去'0'
//            cout<<"j="<<j<<" digit[j]="<<digit[j]<<"\n"<<"i="<<i<<" str[i]="<<str[i]<<"\n";
    }
//       for(int i=length-1;i>=0;i--){
//            cout<<digit[i];
//        }


}
//istream & operator>>(iostream& in,BigInteger& b){
//    string str;
//    in>>str;
//    b=str;
//    return in;
//}
//BigInteger BigInteger::operator-(){}
int main() {
//    string strtest;
//    cin>>strtest;
//    cout<<strtest;
//    string str="892";
//    BigInteger testRes0=str;
//    cout<<testRes0;
//    int a0=297;
//    int b0=233;
//    BigInteger testRes1=a0;
//    BigInteger testRes2=b0;
////    cout<<testRes1;
//    BigInteger res=testRes1+testRes2;
//    cout<<res;
    string a, b;
    while (cin >> a >> b) {
        BigInteger a1 = a;
        BigInteger b1 = b;
        BigInteger answer;
        if (a1 <= b1) {
            answer = a1 + b1;
        } else {
            answer = b1 + a1;
        }

//    cout<<answer.length;
        cout << answer;
    }
    return 0;
}

全部评论

相关推荐

06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务