Advertisement
Spocoman

07. Trekking Mania

Sep 7th, 2023
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     int numberOfGroups;
  8.     cin >> numberOfGroups;
  9.     int musala = 0, montBlanc = 0, kilimanjaro = 0, k2 = 0, everest = 0;
  10.     int totalClimbersSum = 0;
  11.  
  12.     for (int i = 0; i < numberOfGroups; i++) {
  13.         int currentGroupPeople;
  14.         cin >> currentGroupPeople;
  15.  
  16.         totalClimbersSum += currentGroupPeople;
  17.  
  18.         if (currentGroupPeople > 40) {
  19.             everest += currentGroupPeople;
  20.         }
  21.         else if (currentGroupPeople > 25) {
  22.             k2 += currentGroupPeople;
  23.         }
  24.         else if (currentGroupPeople > 12) {
  25.             kilimanjaro += currentGroupPeople;
  26.         }
  27.         else if (currentGroupPeople > 5) {
  28.             montBlanc += currentGroupPeople;
  29.         }
  30.         else if (currentGroupPeople > 0) {
  31.             musala += currentGroupPeople;
  32.         }
  33.     }
  34.  
  35.     cout.setf(ios::fixed);
  36.     cout.precision(2);
  37.  
  38.     cout << (100.0 * musala / totalClimbersSum) << '%' << endl;
  39.     cout << (100.0 * montBlanc / totalClimbersSum) << '%' << endl;
  40.     cout << (100.0 * kilimanjaro / totalClimbersSum) << '%' << endl;
  41.     cout << (100.0 * k2 / totalClimbersSum) << '%' << endl;
  42.     cout << (100.0 * everest / totalClimbersSum) << '%' << endl;
  43.    
  44.     return 0;
  45. }
  46.  
  47. Решение с речник, тернарен оператор и foreach:
  48.  
  49. #include <iostream>
  50. #include <string>
  51. #include <map>
  52.  
  53. using namespace std;
  54.  
  55. int main() {
  56.     map < string, int> peakDestination = {
  57.         {"musala", 0},
  58.         {"montBlanc", 0},
  59.         {"kilimanjaro",0},
  60.         {"k2",0},
  61.                 {"everest",0},
  62.  
  63.     };
  64.  
  65.     int numberOfGroups;
  66.     cin >> numberOfGroups;
  67.  
  68.     int totalClimbersSum = 0;
  69.  
  70.     for (int i = 0; i < numberOfGroups; i++) {
  71.         int currentGroupPeople;
  72.         cin >> currentGroupPeople;
  73.  
  74.         totalClimbersSum += currentGroupPeople;
  75.  
  76.         string currentDestination =
  77.             currentGroupPeople > 40 ? "everest" :
  78.             currentGroupPeople > 25 ? "k2" :
  79.             currentGroupPeople > 12 ? "kilimanjaro" :
  80.             currentGroupPeople > 5 ? "montBlanc" : "musala";
  81.  
  82.         peakDestination[currentDestination] += currentGroupPeople;
  83.     }
  84.  
  85.     cout.setf(ios::fixed);
  86.     cout.precision(2);
  87.  
  88.     for (string peak : {"musala", "montBlanc", "kilimanjaro", "k2", "everest" }) {
  89.         cout << (100.0 * peakDestination[peak] / totalClimbersSum) << '%' << endl;
  90.     }
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement