UVa12541 - Birthdates(排序)

来源:互联网 发布:图片数据库 编辑:程序博客网 时间:2024/06/01 20:43

Write a program to identify the youngest person and the oldest person in a class.
Input
The number n (1  n  100) in the rst line determines the number of people in a class. The following
n lines contain person's name and his/her birthdate.
The information in each line is of this format:
personN ame dd mm yyyy
where personN ame is a single word less than 15 letters, dd mm yyyy are date, month and year of the
birthdate.
Suppose that no one has the same name or the same birthdate.
Output
Print out 2 lines containing the name of youngest person and oldest person, respectively.
Sample Input
5
Mickey 1 10 1991
Alice 30 12 1990
Tom 15 8 1993
Jerry 18 9 1990
Garfield 20 9 1990
Sample Output
Tom
Jerry

#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <string>using namespace std;const int N = 20;struct Person{    string name;    int year, month, day;    bool operator < (const Person &other) const    {        if (year != other.year) return year > other.year;        if (month != other.month) return month > other.month;        if (day != other.day) return day > other.day;        return name < other.name;    }};vector<Person> persons;void input();void solve();int main(){#ifndef ONLINE_JUDGE    freopen("e:\\uva_in.txt", "r", stdin);#endif    input();    solve();    return 0;}void input(){    int n;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        char name[N];        int d, m, y;        scanf("%s%d%d%d", name, &d, &m, &y);        persons.push_back((Person){name, y, m, d});    }}void solve(){    sort(persons.begin(), persons.end());    printf("%s\n%s\n", persons.front().name.c_str(), persons.back().name.c_str());}


 

 

0 0
原创粉丝点击