Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- int numberOfGroups;
- cin >> numberOfGroups;
- int musala = 0, montBlanc = 0, kilimanjaro = 0, k2 = 0, everest = 0;
- int totalClimbersSum = 0;
- for (int i = 0; i < numberOfGroups; i++) {
- int currentGroupPeople;
- cin >> currentGroupPeople;
- totalClimbersSum += currentGroupPeople;
- if (currentGroupPeople > 40) {
- everest += currentGroupPeople;
- }
- else if (currentGroupPeople > 25) {
- k2 += currentGroupPeople;
- }
- else if (currentGroupPeople > 12) {
- kilimanjaro += currentGroupPeople;
- }
- else if (currentGroupPeople > 5) {
- montBlanc += currentGroupPeople;
- }
- else if (currentGroupPeople > 0) {
- musala += currentGroupPeople;
- }
- }
- cout.setf(ios::fixed);
- cout.precision(2);
- cout << (100.0 * musala / totalClimbersSum) << '%' << endl;
- cout << (100.0 * montBlanc / totalClimbersSum) << '%' << endl;
- cout << (100.0 * kilimanjaro / totalClimbersSum) << '%' << endl;
- cout << (100.0 * k2 / totalClimbersSum) << '%' << endl;
- cout << (100.0 * everest / totalClimbersSum) << '%' << endl;
- return 0;
- }
- Решение с речник, тернарен оператор и foreach:
- #include <iostream>
- #include <string>
- #include <map>
- using namespace std;
- int main() {
- map < string, int> peakDestination = {
- {"musala", 0},
- {"montBlanc", 0},
- {"kilimanjaro",0},
- {"k2",0},
- {"everest",0},
- };
- int numberOfGroups;
- cin >> numberOfGroups;
- int totalClimbersSum = 0;
- for (int i = 0; i < numberOfGroups; i++) {
- int currentGroupPeople;
- cin >> currentGroupPeople;
- totalClimbersSum += currentGroupPeople;
- string currentDestination =
- currentGroupPeople > 40 ? "everest" :
- currentGroupPeople > 25 ? "k2" :
- currentGroupPeople > 12 ? "kilimanjaro" :
- currentGroupPeople > 5 ? "montBlanc" : "musala";
- peakDestination[currentDestination] += currentGroupPeople;
- }
- cout.setf(ios::fixed);
- cout.precision(2);
- for (string peak : {"musala", "montBlanc", "kilimanjaro", "k2", "everest" }) {
- cout << (100.0 * peakDestination[peak] / totalClimbersSum) << '%' << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement