求助20pts,WA+RE
查看原帖
求助20pts,WA+RE
768951
_Chronostatis_楼主2023/11/7 22:02

rt

#include <bits/stdc++.h>

namespace Integer {
const std::string symbol = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <class T = int, int binary = 10> class BigInt {
private:
  T n;
  std::vector<T> a;
  bool f;

public:
  BigInt() { Init(); }
  T &operator[](int i) { return a[i]; }
  void Init() {
    n = f = 1;
    a.clear();
  }
  friend std::istream &operator>>(std::istream &tempStream, BigInt &x) {
    std::string s;
    tempStream >> s;
    x.n = s.size();
    if (s[0] == '-') {
      x.f = 0;
      x.n--, s.erase(0, 1);
    }
    for (int i = 0; i < x.n; i++) {
      x.a.push_back((s[x.n - i - 1] >= 'A' && s[x.n - i - 1] <= 'Z' ? s[x.n - i - 1] - 'A' + 10 : s[x.n - i - 1] - '0'));
    }
    return tempStream;
  }
  friend std::ostream &operator<<(std::ostream &tempStream, BigInt a) {
    for (; a.n > 1 && !a[a.n - 1]; a.n--) {
    }
    if (!a.f) {
      tempStream << '-';
    }
    for (int i = a.n - 1; i >= 0; i--) {
      tempStream << symbol[a[i]];
    }
    return tempStream;
  }
  void operator=(int x) {
    Init();
    if (!x) {
      return;
    }
    if (x < 0) {
      x = -x, f = 0;
    }
    n = 0;
    while (x) {
      a.push_back(x % binary);
      n++;
      x /= binary;
    }
  }
  void operator=(std::string x) {
    Init();
    int st = 0;
    if (x[0] == '-') {
      f = 0, st++;
    }
    n = 0;
    int len = x.size();
    for (int i = st; i < len; i++) {
      a.push_back((x[len - i - 1] >= '0' && x[len - i - 1] <= '9' ? x[len - i - 1] - '0' : x[len - i - 1] - 'A' + 10));
      n++;
    }
  }
  void operator=(BigInt x) {
    Init();
    n = x.n;
    f = x.f;
    for (int i = 0; i < n; i++) {
      a[i] = x[i];
    }
  }
  bool operator==(BigInt x) {
    if (n != x.n) {
      return 0;
    }
    for (int i = n - 1; i >= 0; i++) {
      if (a[i] != x[i]) {
        return 0;
      }
    }
    return 1;
  }
  bool operator!=(BigInt x) { return !operator==(x); }
  bool operator<(BigInt x) {
    if (n == x.n) {
      for (int i = n - 1; i >= 0; i--) {
        if (a[i] != x[i]) {
          return a[i] < x[i];
        }
      }
    }
    return n < x.n;
  }
  bool operator>(BigInt x) {
    if (n == x.n) {
      for (int i = n - 1; i >= 0; i--) {
        if (a[i] != x[i]) {
          return a[i] > x[i];
        }
      }
    }
    return n > x.n;
  }
  bool operator<=(BigInt x) { return !operator>(x); }
  bool operator>=(BigInt x) { return !operator<(x); }
  bool operator!() {
    if (n != 1) {
      return 0;
    }
    return !a[0];
  }
  bool operator==(int x) {
    BigInt y;
    y = x;
    return operator==(y);
  }
  bool operator!=(int x) {
    BigInt y;
    y = x;
    return operator!=(y);
  }
  bool operator<(int x) {
    BigInt y;
    y = x;
    return operator<(y);
  }
  bool operator>(int x) {
    BigInt y;
    y = x;
    return operator>(y);
  }
  bool operator<=(int x) {
    BigInt y;
    y = x;
    return operator<=(y);
  }
  bool operator>=(int x) {
    BigInt y;
    y = x;
    return operator>=(y);
  }
  BigInt operator-(BigInt x) {
    BigInt y, z = *this;
    if (z < x) {
      y.f = 0, std::swap(z, x);
    }
    y.n = z.n;
    y.a.resize(z.n);
    for (int i = 0; i < y.n; i++) {
      y[i] = z[i] - x[i];
    }
    for (int i = 0; i < y.n - 1; i++) {
      if (y[i] < 0) {
        y[i + 1]--;
        y[i] += binary;
      }
    }
    for (; !y[y.n - 1] && y.n > 1; y.n--) {
    }
    return y;
  }
  void operator-=(BigInt x) {
    BigInt z = *this;
    *this = z - x;
  }
  void operator-=(int x) {
    BigInt z = *this;
    *this = z - x;
  }
  BigInt operator-(int x) {
    BigInt y;
    y = x;
    return operator-(y);
  }
};
};

using namespace std;
using namespace Integer;

int main() {
  BigInt<> a, b;
  cin >> a >> b;
  cout << a - b;
  return 0;
}
2023/11/7 22:02
加载中...