题解 | 日志排序
日志排序
https://www.nowcoder.com/practice/0f64518fea254c0187ccf0ea05019672
/*
抄的“汀州鹤眺”的代码,难难难
key:
1)struct思想,记录一整条记录,内部再拆分细节
2)学习myCmp的写法
3)getline的使用:读取包含空格的一整行,一般是换行符结束
亦可:cin.limits(numeric_limits<stream>::max(), "\n"); getline(cin, s);
4) 通过“istringstream”对一整行string进行按部分拆解(一般空格隔开?)
n[i].all = "hs_10000_p 2007-01-17 19:22:53,315 253.035(s)"
istringstream stream(n[i].all);
stream >> n[i].name;
stream >> n[i].start1;
stream >> n[i].start2;
stream >> n[i].time;
*/
#include <cstdio>
#include <cstring> // strlen()
#include <iostream>
#include <istream>
#include <sstream>
#include <stdio.h> // gets()
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
struct Node {
string all;
string name;
string start1;
string start2;
double time;
};
bool mySort (const Node &a, const Node &b) {
if (a.time != b.time) {
return a.time<b.time;
}
else if (a.start1 != b.start1) {
return a.start1 < b.start1;
}
else {
return (a.start2 < b.start2);
}
}
int main() {
Node n[10000];
int i = 0;
while (getline(cin,n[i].all) && n[i].all.length()) {
// sscanf(n[i].all, "%s%s%s%lf", n[i].name, n[i].start1, n[i].start2, n[i].time);
istringstream stream(n[i].all);
stream >> n[i].name;
stream >> n[i].start1;
stream >> n[i].start2;
stream >> n[i].time;
i++;
}
sort(n, n+i, mySort);
for (int j = 0; j<i; j++) {
cout << n[j].all << endl;
}
}
// 64 位输出请用 printf("%lld")
