题解 | #日志排序#

日志排序

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

在不知道istringstream类的情况下,处理字符串略显费劲

#include <iostream>
#include <algorithm>
#include "cstdlib"
#include "vector"
using namespace std;

struct task {
    string date;
    string time;
    double range;
    string logLine;
};
bool myCompare(task a, task b) {
    if (a.range == b.range) {
        if (a.date == b.date) return a.time < b.time;
        return a.date < b.date;
    }
    return a.range < b.range;
}
int main() {
    string line;
    vector<task> tasks;
    while (getline(cin, line)) { // 注意 while 处理多个 case
        task temp;
        temp.logLine = line;
        int pos = line.find(' ');
        line = line.substr(pos);
        while (line[0] == ' ') {
            line = line.substr(1);
        }
        temp.date=line.substr(0,10);
        temp.time=line.substr(11,12);
        line=line.substr(23);
        while (line[0] == ' ') {
            line = line.substr(1);
        }
        pos=line.find('(');
        temp.range=atof(line.substr(0,pos).data());
        tasks.push_back(temp);
    }
    sort(tasks.begin(),tasks.end(),myCompare);
    for(auto task:tasks){
        cout<<task.logLine<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

秋国🐮🐴:拿到你简历编号然后让你知道世间险恶
点赞 评论 收藏
分享
黑皮白袜臭脚体育生:春节刚过就开卷吗?哈基馆,你这家伙......
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务