Advertisement
Spocoman

07. Football League

Sep 8th, 2023
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int stadiumCapacity, fanCount, sectorA = 0, sectorB = 0, sectorV = 0, sectorG = 0;
  7.     cin >> stadiumCapacity >> fanCount;
  8.  
  9.     char currentFanSector;
  10.  
  11.     for (int i = 0; i < fanCount; i++) {
  12.         cin >> currentFanSector;
  13.  
  14.         if (currentFanSector == 'A') {
  15.             sectorA++;
  16.         }
  17.         else if (currentFanSector == 'B') {
  18.             sectorB++;
  19.         }
  20.         else if (currentFanSector == 'V') {
  21.             sectorV++;
  22.         }
  23.         else {
  24.             sectorG++;
  25.         }
  26.     }
  27.    
  28.     cout.setf(ios::fixed);
  29.     cout.precision(2);
  30.  
  31.     cout << 100.0 * sectorA / fanCount << "%\n";
  32.     cout << 100.0 * sectorB / fanCount << "%\n";
  33.     cout << 100.0 * sectorV / fanCount << "%\n";
  34.     cout << 100.0 * sectorG / fanCount << "%\n";
  35.     cout << 100.0 * fanCount / stadiumCapacity << "%\n";
  36.  
  37.     return 0;
  38. }
  39.  
  40. Решение с map, foreach и printf():
  41.  
  42. #include <iostream>
  43. #include <map>
  44. #include <string>
  45.  
  46. using namespace std;
  47.  
  48. int main() {
  49.     map <char, int> sectors = { {'A',0},{'B',0},{'V',0},{'G',0} };
  50.  
  51.     int stadiumCapacity, fanCount;
  52.     cin >> stadiumCapacity >> fanCount;
  53.  
  54.     char currentFanSector;
  55.  
  56.     for (int i = 0; i < fanCount; i++) {
  57.         cin >> currentFanSector;
  58.  
  59.         sectors[currentFanSector]++;
  60.     }
  61.    
  62.     for (char sector : {'A', 'B', 'V', 'G'}) {
  63.         printf("%.2f%%\n", 100.0 * sectors[sector] / fanCount);
  64.     }
  65.    
  66.     printf("%.2f%%\n", 100.0 * fanCount / stadiumCapacity);
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement