Advertisement
Trawka011

Untitled

Dec 3rd, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class CandyBox {
  5. private:
  6.     int cnt;
  7.     int* candies;
  8. public:
  9.     CandyBox(int cnt = 0) {
  10.         this->cnt = cnt;
  11.         candies = new int[cnt];
  12.     }
  13.     ~CandyBox() {
  14.         delete[] candies;
  15.     }
  16.     CandyBox(const CandyBox& other) {
  17.         cnt = other.cnt;
  18.         candies = new int[cnt];
  19.         for (int i = 0; i < cnt; i++) {
  20.             candies[i] = other.candies[i];
  21.         }
  22.     }
  23.     CandyBox operator[] (int index) const {
  24.         return candies[index];
  25.     }
  26.     int count(int x) {
  27.         int sum = 0;
  28.         for (int i = 0; i < cnt; i++) {
  29.             if (candies[i] == x)
  30.                 sum++;
  31.         }
  32.         return sum;
  33.     }
  34.     void read() {
  35.         for (int i = 0; i < cnt; i++) {
  36.             cin >> candies[i];
  37.         }
  38.     }
  39.     void print() {
  40.         for (int i = 0; i < cnt; i++) {
  41.             cout << candies[i] << " ";
  42.         }
  43.     }
  44.     int varieties() {
  45.         int sum = 1;
  46.         for (int i = 0; i < cnt; i++) {
  47.             for (int j = 1; j < cnt; j++) {
  48.                 if (candies[j] < candies[j - 1]) {
  49.                     swap(candies[j], candies[j - 1]);
  50.  
  51.                 }
  52.             }
  53.         }
  54.         for (int i = 1; i < cnt; i++) {
  55.             if (candies[i] != candies[i - 1]) {
  56.                 sum++;
  57.             }
  58.         }
  59.         return sum;
  60.     }
  61.     int size() {
  62.         this->cnt = cnt;
  63.         return cnt;
  64.     }
  65.     int& at(int index) {
  66.         return candies[index];
  67.     }
  68. };
  69.  
  70. CandyBox Arcady_process(CandyBox& b) {
  71.     int* arr = new int[b.size()];
  72.     for (int i = 0; i < b.size(); i++) {
  73.         arr[i] = 0;
  74.     }
  75.     int tmp = 0;
  76.     for (int i = 0; i < b.size(); i++) {
  77.         if (b.at(i) % 2 == 0) {
  78.             arr[tmp] = b.at(i);
  79.             b.at(i) = 0;
  80.             tmp++;
  81.         }
  82.     }
  83.     int j = 0;
  84.     for (int i = 0; i < b.size(); i++) {
  85.         if (b.at(i) == 0 && arr[j] != 0) {
  86.             b.at(i) = arr[j];
  87.             j += 2;
  88.         }
  89.     }
  90.     delete[] arr;
  91.     return b;
  92. }
  93.  
  94. CandyBox Alice_process(CandyBox& b) {
  95.     for (int i = 0; i < b.size() / 2; i++) {
  96.         b.at(i) = 0;
  97.     }
  98.     CandyBox c(b);
  99.     for (int i = 0; i < b.size(); i++) {
  100.         b.at(i) = 0;
  101.     }
  102.     int j = b.size()/2;
  103.     for (int i = 0; i < b.size(); i += 2) {
  104.         b.at(i) = c.at(j);
  105.         j++;
  106.     }
  107.     return b;
  108. }
  109.  
  110. int count_afterwards(CandyBox& b, int x) {
  111.     CandyBox a(b);
  112.     Alice_process(a);
  113.     Arcady_process(b);
  114.     return a.count(x) + b.count(x);
  115. }
  116.  
  117. int main() {
  118.    
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement