题解 | #日志排序#
日志排序
https://www.nowcoder.com/practice/0f64518fea254c0187ccf0ea05019672
#include <iostream>
#include <algorithm>
#include <sstream>
#include "cstdlib"
#include "vector"
#include "sstream"
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(' ');//不使用istringstream的解法
// 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());
istringstream lineStream(line);
string name;
lineStream>>name>>temp.date>>temp.time>>temp.range;
tasks.push_back(temp);
}
sort(tasks.begin(),tasks.end(),myCompare);
for(auto task:tasks){
cout<<task.logLine<<endl;
}
}
// 64 位输出请用 printf("%lld")
