当前位置: 代码迷 >> 综合 >> PAT B1028 踩的坑
  详细解决方案

PAT B1028 踩的坑

热度:22   发布时间:2024-02-23 17:39:21.0

有可能存在所有人的日期都在不合法区间内的情况,这是必须特判输出0,否则会因为输出多余的空格而返回“格式错误”

#include<cstdio>
struct person{
    char name[6];int yy;int mm;int dd;
}yougest, oldest, temp, left, right;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 if(a.dd != b.dd) 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 if(a.dd != b.dd) return a.dd >= b.dd;
}void init(){
    oldest.yy = right.yy =  2014;yougest.yy = left.yy = 1814;yougest.mm = right.mm = oldest.mm = left.mm = 9;yougest.dd = right.dd = oldest.dd = left.dd = 6;
}int main(){
    int n;scanf("%d", &n);int num=0;init();for(int i=0;i<n;i++){
    scanf("%s %d/%d/%d", temp.name, &temp.yy, &temp.mm, &temp.dd);if(MoreEqu(temp, left)&&LessEqu(temp, right)){
    num++;if(MoreEqu(temp, yougest)){
    yougest = temp;}if (LessEqu(temp, oldest)){
    oldest = temp;}}} if(num){
    printf("%d %s %s", num, oldest.name, yougest.name);} else{
    **printf("0");**}return 0;
}