题解 | 体重排序
体重排序
https://www.nowcoder.com/practice/28a03ebdd3eb490dba057f7c15ae8610
要考虑排序时间的问题
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct stu {
double w;
string name ;
};
bool cmp (stu& a, stu& b) {
if (a.w != b.w ) {
return a.w < b.w;
}
else {
return a.name <b.name;
}
}
int main() {
vector<stu> student;
stu temp ;
int index;
while (cin >> index ) { // 注意 while 处理多个 case
student.clear();
for (int i = 0; i < index; i++) {
cin >> temp.name >> temp.w;
student.push_back(temp);
}
// sort
sort (student .begin() ,student.end() ,cmp );
//cout
for (int n = 0; n < index ; n++) {
if (n > 0) cout << ' ';
cout << student[n].name;
}
cout << '\n';
}
}
// 64 位输出请用 printf("%lld")
查看10道真题和解析