Advertisement
kutuzzzov

Урок 4 идиома...

May 22nd, 2023
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. template <typename Iterator>
  10. class IteratorRange {
  11. public:
  12.     IteratorRange(Iterator begin, Iterator end)
  13.         : first(begin)
  14.         , last(end) {
  15.     }
  16.  
  17.     Iterator begin() const {
  18.         return first;
  19.     }
  20.  
  21.     Iterator end() const {
  22.         return last;
  23.     }
  24.  
  25. private:
  26.     Iterator first, last;
  27. };
  28.  
  29. template <typename Collection>
  30. auto Head(Collection& v, size_t top) {
  31.     return IteratorRange{v.begin(), next(v.begin(), min(top, v.size()))};
  32. }
  33.  
  34. struct Person {
  35.     string name;
  36.     int age, income;
  37.     bool is_male;
  38. };
  39.  
  40. vector<Person> ReadPeople(istream& input) {
  41.     int count;
  42.     input >> count;
  43.  
  44.     vector<Person> result(count);
  45.     for (Person& p : result) {
  46.         char gender;
  47.         input >> p.name >> p.age >> p.income >> gender;
  48.         p.is_male = gender == 'M';
  49.     }
  50.  
  51.     return result;
  52. }
  53.  
  54. int main() {
  55.     vector<Person> people = ReadPeople(cin);
  56.  
  57.     sort(people.begin(), people.end(),
  58.         [](const Person& lhs, const Person& rhs) {
  59.         return lhs.age < rhs.age;
  60.     });
  61.  
  62.     for (string command; cin >> command;) {
  63.         if (command == "AGE"s) {
  64.             int adult_age;
  65.             cin >> adult_age;
  66.  
  67.             auto adult_begin = lower_bound(people.begin(), people.end(), adult_age,
  68.                 [](const Person& lhs, int age) {
  69.                 return lhs.age < age;
  70.             });
  71.  
  72.             cout << "There are "s << distance(adult_begin, people.end()) << " adult people for maturity age "s
  73.                  << adult_age << '\n';
  74.         } else if (command == "WEALTHY"s) {
  75.             int count;
  76.             cin >> count;
  77.  
  78.             auto head = Head(people, count);
  79.  
  80.             partial_sort(head.begin(), head.end(), people.end(),
  81.                 [](const Person& lhs, const Person& rhs) {
  82.                 return lhs.income > rhs.income;
  83.             });
  84.  
  85.             int total_income = accumulate(head.begin(), head.end(), 0, [](int cur, const Person& p) {
  86.                 return p.income + cur;
  87.             });
  88.             cout << "Top-"s << count << " people have total income "s << total_income << '\n';
  89.         } else if (command == "POPULAR_NAME"s) {
  90.             char gender;
  91.             cin >> gender;
  92.  
  93.             IteratorRange range{people.begin(), partition(people.begin(), people.end(),
  94.                                 [gender](const Person& p) {
  95.                                     return p.is_male == (gender == 'M');
  96.                                 })};
  97.             if (range.begin() == range.end()) {
  98.                 cout << "No people of gender "s << gender << '\n';
  99.             } else {
  100.                 sort(range.begin(), range.end(),
  101.                     [](const Person& lhs, const Person& rhs) {
  102.                     return lhs.name < rhs.name;
  103.                 });
  104.                 string* most_popular_name = &range.begin()->name;
  105.                 int count = 1;
  106.                 for (auto i = range.begin(); i != range.end();) {
  107.                     auto same_name_end = find_if_not(i, range.end(),
  108.                         [i](const Person& p) {
  109.                         return p.name == i->name;
  110.                     });
  111.                     auto cur_name_count = distance(i, same_name_end);
  112.                     if (cur_name_count > count) {
  113.                         count = cur_name_count;
  114.                         most_popular_name = &i->name;
  115.                     }
  116.                     i = same_name_end;
  117.                 }
  118.                 cout << "Most popular name among people of gender "s << gender << " is "s << *most_popular_name << '\n';
  119.             }
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement