-
Notifications
You must be signed in to change notification settings - Fork 0
/
PAT-Basic-1018.cpp
72 lines (67 loc) · 1.75 KB
/
PAT-Basic-1018.cpp
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
#include <iostream>
#include <algorithm>
using namespace std;
struct people{
string name;
int year;
int month;
int day;
};
bool comp(people a,people b){
if(a.year!=b.year) return a.year>b.year;
if(a.month!=b.month) return a.month>b.month;
return a.day>b.day;
}
bool isValid(people a){
if(a.year<1814 || a.year==1814&&a.month<9 || a.year==1814&&a.month==9&&a.day<6
|| a.year>2014 || a.year==2014&&a.month>9 || a.year==2014&&a.month==9&&a.day>6) return false;
return true;
}
int main(){
int N;
cin>>N;
people* p=new people[2];
p[0].year=2014;p[0].month=9;p[0].day=6;
p[1].year=1814;p[1].month=9;p[1].day=6;
int count=0;
//string old_name,young_name;
for(int i=0;i<N;i++){
string str;
people tmp;
cin>>tmp.name>>str;
tmp.year=atoi(str.substr(0,4).c_str());
tmp.month=atoi(str.substr(5,2).c_str());
tmp.day=atoi(str.substr(8,2).c_str());
if(isValid(tmp)){
count++;
if(comp(tmp,p[1])){
p[1].name=tmp.name;
p[1].year=tmp.year;
p[1].month=tmp.month;
p[1].day=tmp.day;
}
if(comp(p[0],tmp)){
p[0].name=tmp.name;
p[0].year=tmp.year;
p[0].month=tmp.month;
p[0].day=tmp.day;
}
}
}
// 一开始用的people数组有N个元素,造成段错误(内存溢出)。其实people数组只要有2个元素就行,存最老和最年轻的
/*sort(p,p+N,comp);
int count=0,old,young;
bool flag=false;
for(int i=0;i<N;i++){
if(isValid(p[i])){
count++;
if(!flag){
young=i;
flag=true;
}
else old=i;
}
}*/
if(count==0) cout<<count<<endl;// 注意特殊情况:没有有效生日
else cout<<count<<" "<<p[0].name<<" "<<p[1].name<<endl;
}