牛客网_PAT乙级_1018人口普查(20)
生活随笔
收集整理的這篇文章主要介紹了
牛客网_PAT乙级_1018人口普查(20)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
某城鎮進行人口普查,得到了全體居民的生日。現請你寫個程序,找出鎮上最年長和最年輕的人。
這里確保每個輸入的日期都是合法的,但不一定是合理的——假設已知鎮上沒有超過200歲的老人,而今天是2014年9月6日,所以超過200歲的生日和未出生的生日都是不合理的,應該被過濾掉。
輸入描述:
輸入在第一行給出正整數N,取值在(0, 105];隨后N行,每行給出1個人的姓名(由不超過5個英文字母組成的字符串)、以及
按“yyyy/mm/dd”(即年/月/日)格式給出的生日。題目保證最年長和最年輕的人沒有并列。
輸出描述:
在一行中順序輸出有效生日的個數、最年長人和最年輕人的姓名,其間以空格分隔。
輸入例子:
5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20
輸出例子:
3 Tom John
代碼
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; class People { public:string name;int year;int month;int date;People(string, int, int, int); }; //構造器 People::People(string name, int year, int month, int date) {this->name = name;this->year = year;this->month = month;this->date = date; } //排序函數 int compareYear(People num1, People num2) {return (num1.year < num2.year); } int compareMonth(People num1, People num2) {return (num1.month < num2.month); } int compareDate(People num1, People num2) {return (num1.date < num2.date); }int main() {int num;//實際人數int n;string name;int year, month, date;cin >> num;std::vector <People> people;//輸入for (n = 0; n < num; n++){cin >> name;cin >> year; cin.get();cin >> month; cin.get();cin >> date;//2014.09.06if (year < 2014 - 200 || (year == 2014 - 200 && month < 9) || (year == 2014 - 200 && month == 9 && date < 6)){continue;}else if (year > 2014 || (year == 2014 && month > 9) || (year == 2014 && month == 9 && date > 6)){continue;}else people.push_back(People(name, year, month, date));//使用構造器}//比較stable_sort(people.begin(), people.end(), compareDate);stable_sort(people.begin(), people.end(), compareMonth);stable_sort(people.begin(), people.end(), compareYear);//輸出int len = people.size();cout << len << " " << people[0].name << " " << people[len - 1].name;system("pause"); } 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的牛客网_PAT乙级_1018人口普查(20)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客网_PAT乙级_1017打印沙漏(2
- 下一篇: C++ vector容器 find er