题解 | #日志排序#

日志排序

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")

全部评论

相关推荐

牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
争当牛马还争不上
码农索隆:1.把简历改哈 2.猛投,狠投 3.把基础打牢 这样你在有机会的时候,才能抓住
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务