PAT B1028 人口普查(20)
课本AC代码
#include <cstdio>
struct person {
char name[10];
int yy, mm, dd;
} oldest, youngest, left, right, temp;
bool LessEqu(person a, person b) {
if(a.yy != b.yy) return a.yy <= b.yy;
else if(a.mm != b.mm) return a.mm <= b.mm;
else return a.dd <= b.dd;
}
bool MoreEqu(person a, person b) {
if(a.yy != b.yy) return a.yy >= b.yy;
else if(a.mm != b.mm) return a.mm >= b.mm;
else return a.dd >= b.dd;
}
void init() {
youngest.yy = left.yy = 1814;
oldest.yy = right.yy = 2014;
youngest.mm = oldest.mm = left.mm = right.mm = 9;
youngest.dd = oldest.dd = left.dd = right.dd = 6;
}
int main() {
init();
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, num = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%s %d/%d/%d", temp.name, &temp.yy, &temp.mm, &temp.dd);
int n = 0, m = 0;
if(MoreEqu(temp, left) && LessEqu(temp, right)) {
num++;
if(LessEqu(temp, oldest)) oldest = temp;
if(MoreEqu(temp, youngest)) youngest = temp;
}
}
if(num == 0) printf("0\n");
else printf("%d %s %s\n", num, oldest.name, youngest.name);
return 0;
}
自己的,两个没过
#include <cstdio>
#include <cstring>
const int nowyear = 2014;
const int nowmonth = 9;
const int nowday = 6;
const int oldyear = 2014 - 200;
struct People {
char name[50];
int year, month, day;
} temp, oldest, youngest, inleft, inright;
bool right(People a, People b) { //判断出生日期是否比现在早
if(a.year == b.year) {
if(a.month == b.month) {
if(a.day == b.day) return true;
else return b.day > a.day;
} else return b.month > a.month;
} else return b.year > a.year;
}
bool left(People a, People b) { //判断出生日期是否大于最早日期
if(a.year == b.year) {
if(a.month == b.month) {
if(a.day == b.day) return true;
else return a.day > b.day; //a.day - b.day;
} else return a.month > b.month; //a.month - b.month;
}else return a.year > b.year; //a.year - b.year;
}
bool judge(People a) { //判断日期是否合理
if(left(inright, a) && right(inleft, a)) return true;
else return false;
}
void init() {
oldest.year = inright.year = nowyear;
youngest.year = inleft.year = nowyear - 200;
oldest.month = youngest.month = inright.month = inleft.month = nowmonth;
oldest.day = youngest.day = inright.month = inleft.day = nowday;
}
int main() {
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
init();
int n; //生日的个数
int a = 0; //有效生日个数
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%s %d/%d/%d", temp.name, &temp.year, &temp.month, &temp.day);
//printf("%s %d/%d/%d\n", temp.name, temp.year, temp.month, temp.day);
int n = 0, m = 0;
// n = right()
if(judge(temp)) {
a++; //有效生日个数加1
if(right(youngest, temp)) youngest = temp;
if(left(oldest, temp)) oldest = temp;
//printf("youngest:%s oldest:%s\n", youngest.name, oldest.name);
}
}
if(a == 0) printf("0\n");
else printf("%d %s %s", a, oldest.name, youngest.name);
return 0;
}
吾生也有涯,而知也无涯。