Advertisement
NickAndNick

Finding the maximum product of two numbers

Apr 10th, 2024 (edited)
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | Software | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Pair {
  6.     using value_t = long;
  7.     using pair_t = pair<value_t, value_t>;
  8.     using return_t = long long;
  9.     pair_t pair{};
  10.     Pair(const pair_t& pair) : pair(pair) {
  11.         if (this->pair.first > this->pair.second) {
  12.             swap(this->pair.first, this->pair.second);
  13.         }
  14.     }
  15.     ~Pair() {}
  16.     return_t multiply() const {
  17.         return (return_t)pair.first * (return_t)pair.second;
  18.     }
  19.     virtual void insert(value_t value) = 0;
  20. private:
  21.     static friend ostream& operator<<(ostream& out, const Pair& p) {
  22.         return out << p.pair.first << ' ' << p.pair.second;
  23.     }
  24.     static friend bool operator>(const Pair& a, const Pair& b) {
  25.         return a.multiply() > b.multiply();
  26.     }
  27. };
  28.  
  29. struct MaxPair : virtual Pair {
  30.     inline static const auto min = numeric_limits<Pair::value_t>::min();
  31.     MaxPair() : Pair({ min, min }) {}
  32.     void insert(value_t value) override {
  33.         if (value > pair.second) {
  34.             pair.first = pair.second;
  35.             pair.second = value;
  36.         } else if (value > pair.first) {
  37.             pair.first = value;
  38.         }
  39.     }
  40. };
  41.  
  42. struct MinPair : virtual Pair {
  43.     inline static const auto max = numeric_limits<Pair::value_t>::max();
  44.     MinPair() : Pair({ max, max }) {}
  45.     void insert(value_t value) override {
  46.         if (value < pair.first) {
  47.             pair.second = pair.first;
  48.             pair.first = value;
  49.         } else if (value < pair.second) {
  50.             pair.second = value;
  51.         }
  52.     }
  53. };
  54.  
  55. class Sequence {
  56. public:
  57.     Sequence() : length(0) {}
  58.     explicit Sequence(const size_t length) : length(length) {}
  59.     void result() const {
  60.         if (length < 2) puts("No result");
  61.         else {
  62.             const Pair* max = &max_pair;
  63.             const Pair* min = &min_pair;
  64.             cout << (*max > *min ? *max : *min) << '\n';
  65.         }
  66.     }
  67. private:
  68.     size_t length;
  69.     MaxPair max_pair;
  70.     MinPair min_pair;
  71.     static friend istream& operator>>(istream& inp, Sequence& solution) {
  72.         Pair::value_t value{};
  73.         for (size_t i = 0; i < solution.length; ++i) {
  74.             inp >> value;
  75.             solution.max_pair.insert(value);
  76.             solution.min_pair.insert(value);
  77.         }
  78.         return inp;
  79.     }
  80. };
  81.  
  82. static size_t parse(const char* prompt = "") {
  83.     cout << prompt;
  84.     size_t value;
  85.     cin >> value;
  86.     cin.ignore();
  87.     return value;
  88. }
  89.  
  90. int main() {
  91.     const auto quantity = parse();
  92.     Sequence sequence(quantity);
  93.     cin >> sequence;
  94.     sequence.result();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement