Advertisement
kutuzzzov

Урок 3

Feb 15th, 2023
1,684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. class MoneyBox {
  9. public:
  10.     explicit MoneyBox(vector<int64_t> nominals)
  11.         : nominals_(move(nominals))
  12.         , counts_(nominals_.size()) {
  13.     }
  14.  
  15.     const vector<int>& GetCounts() const {
  16.         return counts_;
  17.     }
  18.  
  19.     int GetNominalIdx(int64_t value) {
  20.         return static_cast<int>(find(nominals_.begin(), nominals_.end(), value) - nominals_.begin());
  21.     }
  22.    
  23.     void PushCoin(int64_t value) {
  24.         // реализуйте метод добавления купюры или монеты
  25.         ++counts_[GetNominalIdx(value)];
  26.     }
  27.  
  28.     void PrintCoins(ostream& out) const {
  29.         // реализуйте метод печати доступных средств
  30.         for (int i = 0; i < counts_.size(); ++i) {
  31.             if (counts_[i] > 0) {
  32.                 out << nominals_[i] << ": "s << counts_[i] << endl;
  33.             }
  34.         }
  35.     }
  36.  
  37. private:
  38.     const vector<int64_t> nominals_;
  39.     vector<int> counts_;
  40. };
  41.  
  42. ostream& operator<<(ostream& out, const MoneyBox& cash) {
  43.     cash.PrintCoins(out);
  44.     return out;
  45. }
  46.  
  47. int main() {
  48.     MoneyBox cash({10, 50, 100, 200, 500, 1000, 2000, 5000});
  49.  
  50.     int times;
  51.     cout << "Enter number of coins you have:"s << endl;
  52.     cin >> times;
  53.  
  54.     cout << "Enter all nominals:"s << endl;
  55.     for (int i = 0; i < times; ++i) {
  56.         int64_t value;
  57.         cin >> value;
  58.         cash.PushCoin(value);
  59.     }
  60.  
  61.     cout << cash << endl;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement