Fork me on GitHub
0%

PAT乙级1028人口普查

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。
这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。

Input Specification

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

Output Specification

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

Sample Input

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

Sample Output

3 Tom John

题目大意

筛选合法日期后,输出合法日期数,最大最小日期的人的名称

注意

  • 未考虑边界情况 一个身份都不合理
  • 数组少开了个0

方法一(结构体存储,重写比较函数)

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
//
// PAT乙级1028人口普查.cpp
// Fushi
//
// Created by H C on 2021/2/5.
//

#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
//错因:1、未考虑边界情况 一个身份都不合理 2、数组少开了个0
typedef struct people
{
char name[6];
int year;
int month;
int day;
}peo;

peo persons[100010];

bool judge_unreasonable(int y,int m,int d){
if(y>2014) return true;
if(y==2014&&m>9)return true;
if(y==2014&&m==9&&d>6)return true;
if(y<1814) return true;
if(y==1814&&m<9)return true;
if(y==1814&&m==9&&d<6)return true;

return false;
}

bool cmp(peo A,peo B)
{
if(A.year!=B.year) return A.year<B.year;
else if(A.month!=B.month) return A.month<B.month;
else return A.day<B.day;
}

int main()
{
int N;
scanf("%d",&N);
int count = 0;
for (int i=0; i<N; i++) {
char name[6];
int y,m,d;
scanf("%s %d/%d/%d",name,&y,&m,&d);
bool flag = judge_unreasonable(y, m, d);
if(!flag)
{
strcpy(persons[count].name, name);
persons[count].year = y;
persons[count].month = m;
persons[count].day = d;
count++;
}
}
sort(persons, persons+count, cmp);
// 边界
if(count!=0)
printf("%d %s %s",count,persons[0].name,persons[count-1].name);
else printf("0");
}

方法二(日期哈希)

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
//
// PAT乙级1028人口普查.cpp
// Fushi
//
// Created by H C on 2021/2/5.
//

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
//错因:1、未考虑边界情况 一个身份都不合理 2、数组少开了个0
int main()
{
int n;
scanf("%d",&n);
int cnt=0;
int MAX=18140906,MIN=20140906;
string MAX_name,MIN_name;
for(int i=0;i<n;i++)
{
string name;
int y,m,d;
cin>>name;
scanf("%d/%d/%d",&y,&m,&d);
int tmp = y*10000+m*100+d;

if (tmp<=20140906&&tmp>=18140906) {
cnt++;
if (tmp>=MAX) {
MAX = tmp;
MAX_name = name;
}
if (tmp<=MIN) {
MIN = tmp;
MIN_name = name;
}
}
}
if (cnt==0) {
printf("0");
return 0;
}
else
{
printf("%d %s %s",cnt,MIN_name.c_str(),MAX_name.c_str());
}
}

方法三(String处理日期)

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
//
// PAT乙级1028人口普查.cpp
// Fushi
//
// Created by H C on 2021/2/5.
//

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
//错因:1、未考虑边界情况 一个身份都不合理 2、数组少开了个0
int main()
{
int n;
scanf("%d",&n);
int cnt=0;
string MAX_name,MIN_name;
string MAX = "1814/09/06",MIN = "2014/09/06";
for(int i=0;i<n;i++)
{
string name,tmp;
cin>>name>>tmp;

if (tmp<="2014/09/06"&&tmp>="1814/09/06") {
cnt++;
if (tmp>=MAX) {
MAX = tmp;
MAX_name = name;
}
if (tmp<=MIN) {
MIN = tmp;
MIN_name = name;
}
}
}
if (cnt==0) {
printf("0");
return 0;
}
else
{
printf("%d %s %s",cnt,MIN_name.c_str(),MAX_name.c_str());
}
}

觉得有帮助的请作者喝杯咖啡吧~