题解 | 日志排序

日志排序

https://www.nowcoder.com/practice/0f64518fea254c0187ccf0ea05019672

#include <iostream>
#include<algorithm>
#include<sstream>
using namespace std;

typedef struct taskLog {
    string total_log;
    string begin_date;
    string begin_time;
    float dur;
}taskLog;

struct taskLog t[5000];

bool cmp(taskLog& a, taskLog& b){
    if(a.dur!=b.dur){
        return a.dur<b.dur;
    }
    if(a.begin_date!=b.begin_date){
        return a.begin_date<b.begin_date;
    }
    return a.begin_time<b.begin_time;
}
int main() {
    string s;
    int i = 0;
    while (getline(cin, s)) { // 注意 while 处理多个 case
        if (s == "")break;
        t[i].total_log=s;
        bool flag = false;
        stringstream ss(s);
        string token;
        int cnt=0;
        while(ss>>token){
            if(cnt==1){
                t[i].begin_date=token;
            }
            else if(cnt==2){
                t[i].begin_time=token;                ;
            }
            else if(cnt==3){
                t[i].dur=stof(token.substr(0,token.length()-3));
            }
            cnt++;
        }
        i++;
    }
    sort(t,t+i,cmp);
    for(int j=0;j<i;j++)cout<<t[j].total_log<<endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-06 16:59
已编辑
河南工业大学 Java
牛牛牛的牛子:最好扔了,实在没有选择的选择
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务