您的位置:首页 > 其它

HDOJ1862 EXCEL排序(比较器)

2017-12-13 21:49 295 查看

EXCEL排序

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 20914    Accepted Submission(s): 7600


[align=left]Problem Description[/align]
Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。
 

[align=left]Input[/align]
测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有 N

行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。

 

[align=left]Output[/align]
对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3

时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

 

[align=left]Sample Input[/align]

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
0 0

 

[align=left]Sample Output[/align]

Case 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Case 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Case 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

 题目本身不是很难,就是不能使用普通的排序。
需要使用util.Arrays的:(sort(T[] a,Comparator< ? super T> c)根据指定比较器产生的顺序对指定对象数组进行排序。该排序算法是一个经过修改的合并排序算法(其中,如果低子列表中的最高元素小于高子列表中的最低元素,则忽略合并)。此算法提供可保证的 n*log(n) 性能。
数据量很大,也可以加上StreamTokenizer和PrintWriter的使用加快效率。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Comparator;

public class Main {
public static void main(String[] args) throws IOException{
int cases = 1;
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
in.nextToken();
int c = (int) in.nval;
if (n == 0) {
break;
}
Student student[] = new Student
;
for (int i = 0; i < n; i++) {
student[i] = new Student();
in.nextToken();
student[i].num = (int) in.nval;
in.nextToken();
student[i].name = in.sval;
in.nextToken();
student[i].score = (int) in.nval;
}
if (c == 1) {
sort1(student);
} else if (c == 2) {
sort2(student);
} else if (c == 3) {
sort3(student);
}
// 打印
out.println("Case " + cases + ":");
out.flush();
cases++;
for (int i = 0; i < n; i++) {
out.printf("%06d",student[i].num);
out.flush();
out.println(" "+student[i].name+" "+student[i].score);;
out.flush();
}
}
}

private static void sort1(Student[] students) {
// 根据学号排序
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.num > o2.num) {
return 1;// 返回1就是交换
}
return -1;
}
});
}

private static void sort2(Student[] student) {
// 根据姓名(学号)排序
Arrays.sort(student, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.name.compareTo(o2.name) > 0) {// 按姓名排序
return 1;
}
if (o1.name.compareTo(o2.name) == 0) {
if (o1.num > o2.num) {// 姓名一样,按学号排序
return 1;
}
return -1;
}
return -1;
}
});
}

private static void sort3(Student[] student) {
// 根据成绩(学号)排序
Arrays.sort(student, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.score > o2.score) {
return 1;
}
if (o1.score == o2.score) {
if (o1.num > o2.num) {
return 1;
}
return -1;
}
return -1;
}
});
}
}

class Student {
public int num;
public String name;
public int score;
}


前面StreamTokenizer和PrintWriter的例子可能你没看懂。
你可以看这篇文章,有一些解释:http://blog.csdn.net/qq_38238041/article/details/78648192
也可以使用Scanner的方式:
private static Scanner scanner;

public static void main(String[] args){
int cases = 1;
scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
int c = scanner.nextInt();
if (n == 0) {
break;
}
Student2 student[] = new Student2
;
for (int i = 0; i < n; i++) {
student[i] = new Student2();
student[i].num = scanner.nextInt();
student[i].name = scanner.next();
student[i].score = scanner.nextInt();
}
if (c == 1) {
sort1(student);
} else if (c == 2) {
sort2(student);
} else if (c == 3) {
sort3(student);
}
// 打印
System.out.println("Case " + cases + ":");
cases++;
for (int i = 0; i < n; i++) {
System.out.printf("%06d",student[i].num);
System.out.println(" "+student[i].name+" "+student[i].score);;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: