题解 | #日志排序#

日志排序

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

#include<iostream>
#include<vector>
#include<algorithm>
//hs_10000_p   2007-01-17 19:22:53,315     253.035(s)

std::string Start(std::string s)//提取开始时间字符串
{
    int pos1 = s.find('-');
    std::string s1 = s.substr(pos1-4, 23);
    return s1;
}

double Consume(std::string s)//提取消耗时间并转换为double类型
{
    int pos1 = s.rfind("(s)");
    int pos2 = s.rfind(" ");
    std::string s1 = s.substr(pos2+1, pos1-pos2-1);
    return std::stod(s1);
}

bool Compare(std::string s1, std::string s2)//自己编写比较函数
{
    if(Consume(s1) < Consume(s2))
    {
        return true;
    }else if(Consume(s1) > Consume(s2))
    {
        return false;
    }
    return Start(s1) < Start(s2);//开始时间直接用字符串对比即可
}

int main()
{
    std::string s;
    std::vector<std::string> log;
    while(std::getline(std::cin, s))//使用getline获取整行字符串
    {
        if(s.empty()) break;
        log.push_back(s);
    }
    std::sort(log.begin(), log.end(), Compare);
    for(auto& i : log)
    {
        std::cout << i << std::endl;
    }
    log.clear();
}

全部评论

相关推荐

一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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