Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- int quantity, red = 0, orange = 0, blue = 0, green = 0, maxCount = 0;
- cin >> quantity;
- string color, maxColor = "";
- for (int i = 0; i < quantity; i++) {
- cin >> color;
- if (color == "red") {
- red++;
- }
- else if (color == "orange") {
- orange++;
- }
- else if (color == "blue") {
- blue++;
- }
- else if (color == "green") {
- green++;
- }
- }
- if (red > orange && red > blue && red > green) {
- maxCount = red;
- maxColor = "red";
- }
- else if (orange > blue && orange > green && orange > red) {
- maxCount = orange;
- maxColor = "orange";
- }
- else if (blue > green && blue > red && blue > orange) {
- maxCount = blue;
- maxColor = "blue";
- }
- else {
- maxCount = green;
- maxColor = "green";
- }
- cout << "Red eggs: " << red << endl
- << "Orange eggs: " << orange << endl
- << "Blue eggs: " << blue << endl
- << "Green eggs: " << green << endl
- << "Max eggs: " << maxCount << " -> " << maxColor << endl;
- return 0;
- }
- РЕШЕНИЕ С МАР И FOR(EACH):
- #include <iostream>
- #include <map>
- using namespace std;
- int main() {
- map <string, int> eggInfo = { {"red",0},{"orange",0},{"blue",0},{"green",0} };
- int quantity, maxCount = 0;
- cin >> quantity;
- string color, maxColor = "";
- for (int i = 0; i < quantity; i++) {
- cin >> color;
- eggInfo[color]++;
- }
- for (auto& egg : eggInfo) {
- if (egg.second > maxCount) {
- maxColor = egg.first;
- maxCount = egg.second;
- }
- }
- cout << "Red eggs: " << eggInfo["red"] << endl
- << "Orange eggs: " << eggInfo["orange"] << endl
- << "Blue eggs: " << eggInfo["blue"] << endl
- << "Green eggs: " << eggInfo["green"] << endl
- << "Max eggs: " << maxCount << " -> " << maxColor << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement