PAT 乙级 1028 人口普查(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

方法一:

 1 #include<iostream>
 2 #include<string>
 3 #include<cstdlib>
 4 #include<map>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int N, i, num = 0,low=18140905,high=20140907;
11     int year,month,day,value;
12     char c;
13     string name;
14     map<int, string> m;
15 
16     cin >> N;
17 
18     for (i = 0; i < N; ++i)
19     {
20         cin.get();
21         cin >> name >> year >> c >> month >> c >> day;
22         
23         value=year*10000+month*100+day;
24 
25         if (value > low&&value < high)
26         {
27             m.insert(pair<int, string>(value, name));
28 
29             ++num;
30         }
31     }
32 
33     map<int, string>::iterator max = m.begin();
34     map<int, string>::reverse_iterator min = m.rbegin();
35 
36     if (num > 0)
37         cout << num << " " << max->second << " " << min->second << endl;
38     else
39         cout << 0 << endl;
40 
41     return 0;
42 }

 

 

方法二:用生日类比较

 1 #include<iostream>
 2 #include<string>
 3 #include<cstdlib>
 4 #include<map>
 5 
 6 using namespace std;
 7 
 8 class birthday
 9 {
10 public:
11     int year;
12     int month;
13     int day;
14 public:
15     bool operator>(const birthday& bir) const;
16     bool operator<(const birthday& bir) const;
17 };
18 
19 int main()
20 {
21     int N, i, num = 0;
22     char c;
23     string name;
24     birthday b, low = { 1814, 9, 5 }, high = { 2014, 9, 7 };
25     map<birthday, string> m;
26 
27     cin >> N;
28 
29     for (i = 0; i < N; ++i)
30     {
31         cin.get();
32         cin >> name >> b.year >> c >> b.month >> c >> b.day;
33 
34         if (b > low&&b < high)
35         {
36             m.insert(pair<birthday, string>(b, name));
37 
38             ++num;
39         }
40     }
41 
42     map<birthday, string>::iterator max = m.begin();
43     map<birthday, string>::reverse_iterator min = m.rbegin();
44 
45     if (num > 0)
46         cout << num << " " << max->second << " " << min->second << endl;
47     else
48         cout << 0 << endl;
49 
50     return 0;
51 }
52 
53 //比较出生日期,出生日期大(晚出生)返回1
54 bool birthday::operator>(const birthday& bir) const
55 {
56     if (this->year > bir.year)
57         return 1;
58     else if (this->year == bir.year&&this->month > bir.month)
59         return 1;
60     else if (this->year == bir.year&&this->month == bir.month&&this->day > bir.day)
61         return 1;
62     else
63         return 0;
64 }
65 
66 //比较出生日期,出生日期小(早出生)返回1
67 bool birthday::operator<(const birthday& bir) const
68 {
69     if (this->year < bir.year)
70         return 1;
71     else if (this->year == bir.year&&this->month < bir.month)
72         return 1;
73     else if (this->year == bir.year&&this->month == bir.month&&this->day < bir.day)
74         return 1;
75     else
76         return 0;
77 }

 

posted on 2018-01-03 21:22  么么打123  阅读(162)  评论(0编辑  收藏  举报