Store the date in a string, and then judge one by one, the oldest person is younger than the maximum date (2014/09/06), and the youngest person is older than the minimum date (1814/09/06), and then put the name Save, judge, overwrite the name and date one at a time, and leave the oldest and youngest at the end.
Note: There was a point that didn't pass when I submitted, and the problem displayed was that the format was wrong. Later, I checked the case that count was 0, and found that when the output was 0, there was a space after it, so I added a judgment on whether the count is 0 or not. .
#include <iostream> using namespace std; intmain() { int n; cin >> n; string max = "1814/09/06"; string min = "2014/09/06"; string maxname = min, minname = max; string n1, n2; int count = 0; for(int i = 0; i < n; i++) { string s, day; cin >> s >> day; if(day >= max && day <= min) { count++; if(day <= maxname) { n1 = s; maxname = day; } if(day >= minname){ minname = day; n2 = s; } } } if(count == 0) { cout << count; } else { cout << count << " " << n1 << " " << n2; } return 0; }