P1104 生日 洛谷(结构体排序)

简单的结构体排序,可是最后一组样例总是通不过?看了别人的题解才知道原来编号也得算进排序…题目里面还告诉了…结果WA了几次…果然这是没认真读题的后果…生日从大到小的意味着年份小,相同年分比月份小,相同月份比天数小,相同天数比编号大…代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <utility>
#define ll long long

using namespace std ;

typedef struct{
    string name ;
    int year ;
    int month ;
    int day ;
    int number ;
}meassage ;

bool cmp(meassage x , meassage y){
    if ( x.year == y.year ){
        if ( x.month == y.month ){
            if ( x.day == y.day ){
                return x.number > y.number ;
            }
            return x.day < y.day ;
        }
        return x.month < y.month ;
    }
    return x.year < y.year ;
}

int main(){
    meassage mea[500] ;
    int n ;
    cin >> n ;
    for ( int i = 0 ; i < n ; i ++ ){
        cin >> mea[i].name >> mea[i].year >> mea[i].month >> mea[i].day ;
        mea[i].number = i ;
    }
    sort(mea , mea + n , cmp) ;
    for ( int i = 0 ; i < n ; i ++ ){
        if ( i == n - 1 ){
            cout << mea[i].name ;
        }else{
            cout << mea[i].name << endl ;
        }
    }
    return 0 ;
}
posted @ 2018-10-11 22:12  Cantredo  阅读(369)  评论(0编辑  收藏  举报