题解 | #日志排序#
日志排序
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")
小天才公司福利 1152人发布
