题解 | #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; }