Keshawn_lu's Blog

PAT 1028.人口普查

字数统计: 841阅读时长: 4 min
2021/02/06 Share

题目简介:

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。

输入格式:

输入在第一行给出正整数 N,取值在(0,105];随后 N 行,每行给出 1 个人的姓名(由不超过 5 个英文字母组成的字符串)、以及按 yyyy/mm/dd(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。

输出格式:

在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。

输入样例:

1
2
3
4
5
6
5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

输出样例:

1
3 Tom John

思路:

这题的坑是真的多,自己眼瞎也让自己自闭了半小时。

首先是判断出生日期是否符合标准,不符合不用再考虑下一步了。

然后再通过比较得出年级最大和最小的人,并保存下来。

tips:

  • 当没有一个人符合要求时,直接输出0即可。
  • atoi()函数能直接将07这种字符串转换为7,不用自己再去修正了。
  • 此题不需要考虑月份,日期超过边界的问题。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cmath>
#include <iomanip>

using namespace std;

struct Person
{
string name;
string birth;
};

bool judge(string& birth) {

string year = birth.substr(0, 4);
string month = birth.substr(5, 2);
string day = birth.substr(8, 2);

//if (month[0] == '0')
// month = month.erase(0, 1);
//if (day[0] == '0')
// day = day.erase(0, 1);

//if (atoi(month.c_str()) < 1 || atoi(month.c_str()) > 12)
// return false;

//if (atoi(day.c_str()) < 1 || atoi(day.c_str()) > 30)
// return false;

//if (year[0] == '0') {

// return false;
//}

if (atoi(year.c_str()) < 1814 || atoi(year.c_str()) > 2014)
return false;

if (atoi(year.c_str()) == 1814) {

if (atoi(month.c_str()) < 9)
return false;

if (atoi(month.c_str()) == 9 && atoi(day.c_str()) < 6)
return false;
}

if (atoi(year.c_str()) == 2014) {

if (atoi(month.c_str()) > 9)
return false;

if (atoi(month.c_str()) == 9 && atoi(day.c_str()) > 6)
return false;
}

return true;
}

bool isold(string& p1, string& p2) { //p1是否比p2老

string p1_year = p1.substr(0, 4);
string p1_month = p1.substr(5, 2);
string p1_day = p1.substr(8, 2);

//if (p1_month[0] == '0')
// p1_month = p1_month.erase(0, 1);
//if (p1_day[0] == '0')
// p1_day = p1_day.erase(0, 1);

string p2_year = p2.substr(0, 4);
string p2_month = p2.substr(5, 2);
string p2_day = p2.substr(8, 2);

//if (p2_month[0] == '0')
// p2_month = p2_month.erase(0, 1);
//if (p2_day[0] == '0')
// p2_day = p2_day.erase(0, 1);

if (atoi(p1_year.c_str()) < atoi(p2_year.c_str()))
return true;
if (atoi(p1_year.c_str()) == atoi(p2_year.c_str()) && atoi(p1_month.c_str()) < atoi(p2_month.c_str()))
return true;
if (atoi(p1_year.c_str()) == atoi(p2_year.c_str()) && atoi(p1_month.c_str()) == atoi(p2_month.c_str()) && atoi(p1_day.c_str()) < atoi(p2_day.c_str()))
return true;

return false;
}


int main()
{

int N;
cin >> N;

Person max_old;
max_old.birth = "2014/09/07";

Person max_young;
max_young.birth = "1814/09/05";

int valid_num = 0;
for (int i = 0; i < N; i++) {

Person temp;
cin >> temp.name >> temp.birth;

if (judge(temp.birth)) {

valid_num++;

if (isold(temp.birth, max_old.birth))
max_old = temp;

if (isold(max_young.birth, temp.birth))
max_young = temp;
}
}

if (valid_num == 0) {

cout << valid_num;
return 0;
}

cout << valid_num << ' ' << max_old.name << ' ' << max_young.name;
}
CATALOG
  1. 1. 题目简介:
    1. 1.1. 输入格式:
    2. 1.2. 输出格式:
    3. 1.3. 输入样例:
    4. 1.4. 输出样例:
  2. 2. 思路:
  3. 3. 代码如下: