Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- using namespace std;
- enum class Gender
- {
- FEMALE,
- MALE
- };
- struct Person
- {
- int age;
- Gender gender;
- bool is_employed;
- };
- template <typename InputIt>
- int ComputeMedianAge(InputIt range_begin, InputIt range_end)
- {
- if(range_begin==range_end)
- {
- return 0;
- }
- vector<typename InputIt::value_type> range_copy(range_begin,range_end);
- auto middle = begin(range_copy) + range_copy.size()/2;
- nth_element(begin(range_copy),middle, end(range_copy),[](const Person&lhs, const Person& rhs)
- {return lhs.age<rhs.age;});
- return middle->age;
- }
- void PrintStats(vector<Person> persons)
- {
- auto females_end = partition(begin(persons),end(persons),[](const Person&p)
- {return p.gender == Gender::FEMALE; });
- auto employed_females_end = partition(begin(persons),females_end,[](const Person&p){return p.is_employed; });
- auto employed_males_end = partition(females_end,end(persons),[](const Person&p){return p.is_employed; });
- cout << "Median age = " << ComputeMedianAge(begin(persons), end(persons)) << endl;
- cout << "Median age for females = " << ComputeMedianAge(begin(persons), females_end) <<endl;
- cout << "Median age for males = " << ComputeMedianAge(females_end, end(persons)) << endl;
- cout << "Median age for employed females = " << ComputeMedianAge(begin(persons), employed_females_end) << endl;
- cout << "Median age for unemployed females = " << ComputeMedianAge(employed_females_end, females_end) << endl;
- cout << "Median age for employed males = " << ComputeMedianAge(females_end, employed_males_end) << endl;
- cout << "Median age for unemployed males = " << ComputeMedianAge(employed_males_end, end(persons)) << endl;
- }
- int main()
- {
- int person_count;
- cin >> person_count;
- vector<Person> persons;
- persons.reserve (person_count);
- for(int i=0;i<person_count;++i)
- {
- int age,gender, is_employed;
- cin >> age >> gender >> is_employed;
- Person person {age,static_cast<Gender>(gender),is_employed==1};
- persons.push_back(person);
- }
- PrintStats(persons);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement