题解
简单排下序。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class Student{
String ID;
String name;
int score;
public Student(String iD, String name, int score) {
super();
ID = iD;
this.name = name;
this.score = score;
}
}
class SortWay implements Comparator<Student>{
int way;
SortWay(int c){
way = c;
}
@Override
public int compare(Student o1, Student o2) {
if(way == 1){
return o1.ID.compareTo(o2.ID);
}
else if(way == 2){
if(!o1.name.equals(o2.name)) return o1.name.compareTo(o2.name);
else return o1.ID.compareTo(o2.ID);
}
else{
if(o1.score != o2.score) return o1.score - o2.score;
else return o1.ID.compareTo(o2.ID);
}
}
}
public class Main{
public static void main(String[] args) {
ArrayList<Student> arr = new ArrayList<Student>();
Scanner in = new Scanner(System.in);
int n = in.nextInt(), c = in.nextInt();
for(int i = 0; i < n; ++i){
arr.add(new Student(in.next(), in.next(), in.nextInt()));
}
Collections.sort(arr, new SortWay(c));
for(Student stu : arr){
System.out.println(stu.ID + " " + stu.name + " " + stu.score);
}
}
}
效率是硬伤。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = 100000 + 10;
struct Student{
string ID;
string name;
int score;
Student(){}
Student(string id, string n, int s):ID(id), name(n), score(s){}
};
int n, c;
Student stu[maxn];
int main()
{
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif // LOCAL
cin >> n >> c;
char id[10], name[10];
int score;
for(int i = 0; i < n; ++i){
// cin >> stu[i].ID >> stu[i].name >> stu[i].score;
scanf("%s %s %d", id, name, &score);
stu[i] = Student(id, name, score);
}
if(c == 1){
sort(stu, stu + n, [](Student& s1, Student& s2){ return s1.ID < s2.ID; });
}
else if(c == 2){
sort(stu, stu + n, [](Student& s1, Student& s2){
if(s1.name != s2.name) return s1.name < s2.name;
else return s1.ID < s2.ID;
});
}
else{
sort(stu, stu + n, [](Student& s1, Student& s2){
if(s1.score != s2.score) return s1.score < s2.score;
else return s1.ID < s2.ID;
});
}
for(int i = 0; i < n; ++i){
// cout << stu[i].ID << " " << stu[i].name << " " << stu[i].score << endl;
printf("%s %s %d\n", stu[i].ID.c_str(), stu[i].name.c_str(), stu[i].score);
}
return 0;
}