Advertisement
Spocoman

Easter Eggs

Sep 17th, 2023 (edited)
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int quantity, red = 0, orange = 0, blue = 0, green = 0, maxCount = 0;
  7.     cin >> quantity;
  8.  
  9.     string color, maxColor = "";
  10.  
  11.     for (int i = 0; i < quantity; i++) {
  12.         cin >> color;
  13.         if (color == "red") {
  14.             red++;
  15.         }
  16.         else if (color == "orange") {
  17.             orange++;
  18.         }
  19.         else if (color == "blue") {
  20.             blue++;
  21.         }
  22.         else if (color == "green") {
  23.             green++;
  24.         }
  25.     }
  26.  
  27.     if (red > orange && red > blue && red > green) {
  28.         maxCount = red;
  29.         maxColor = "red";
  30.     }
  31.     else if (orange > blue && orange > green && orange > red) {
  32.         maxCount = orange;
  33.         maxColor = "orange";
  34.     }
  35.     else if (blue > green && blue > red && blue > orange) {
  36.         maxCount = blue;
  37.         maxColor = "blue";
  38.     }
  39.     else {
  40.         maxCount = green;
  41.         maxColor = "green";
  42.     }
  43.  
  44.     cout << "Red eggs: " << red << endl
  45.         << "Orange eggs: " << orange << endl
  46.         << "Blue eggs: " << blue << endl
  47.         << "Green eggs: " << green << endl
  48.         << "Max eggs: " << maxCount << " -> " << maxColor << endl;
  49.  
  50.     return 0;
  51. }
  52.  
  53. РЕШЕНИЕ С МАР И FOR(EACH):
  54.  
  55. #include <iostream>
  56. #include <map>
  57.  
  58. using namespace std;
  59.  
  60. int main() {
  61.     map <string, int> eggInfo = { {"red",0},{"orange",0},{"blue",0},{"green",0} };
  62.  
  63.     int quantity, maxCount = 0;
  64.     cin >> quantity;
  65.  
  66.     string color, maxColor = "";
  67.  
  68.     for (int i = 0; i < quantity; i++) {
  69.         cin >> color;
  70.         eggInfo[color]++;
  71.     }
  72.  
  73.     for (auto& egg : eggInfo) {
  74.         if (egg.second > maxCount) {
  75.             maxColor = egg.first;
  76.             maxCount = egg.second;
  77.         }
  78.     }
  79.  
  80.     cout << "Red eggs: " << eggInfo["red"] << endl
  81.         << "Orange eggs: " << eggInfo["orange"] << endl
  82.         << "Blue eggs: " << eggInfo["blue"] << endl
  83.         << "Green eggs: " << eggInfo["green"] << endl
  84.         << "Max eggs: " << maxCount << " -> " << maxColor << endl;
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement