Excel can sort records according to any column. Now you are supposed to imitate this function.
Excel可以根据任何列对记录进行排序。现在你应该模仿这个函数。
Input Specification:
Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student's record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).
每个输入文件包含一个测试用例。对于每种情况,第一行包含两个整数N(≤105)和C,其中N是记录的数量,C是应该对记录进行排序的列。接下来是N行,每行包含一个学生的记录。学生的记录包括他或她的不同ID(一个6位数的数字)、姓名(一个不超过8个字符且没有空格的字符串)和成绩(0到100之间的整数,包括0和100)。
Output Specification:
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID's in increasing order.
对于每个测试用例,以N行输出排序结果。也就是说,如果C=1,则必须根据ID按递增顺序对记录进行排序;如果C=2,则记录必须根据名称按非递减顺序排序;如果C=3,则记录必须根据等级按非递减顺序排序。如果有几个学生的名字或年级相同,必须根据他们的身份证按升序排列。
Sample Input 1:
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2:
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3:
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 9
Sample Output 3:
000002 James 9
000001 Zoe 60
000007 James 85
000010 Amy 90
思路:这个题没有什么算法之类的,就是排序,但很可惜用Java最后一个测试点运行超时.....
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Main{
public static void main(String[] args)throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(bf);
in.nextToken();
int n = (int) in.nval;
in.nextToken();
int c = (int) in.nval;
student []arr = new student[n];
for(int i = 0 ; i < n ;i++)
{
String []str = bf.readLine().split(" ");
student s = new student();
s.id = str[0] ;
s.name = str[1];
s.grade = Integer.parseInt(str[2]);
arr[i] = s;
}
if(c == 1)
{
Arrays.sort(arr, new Comparator<student>() {
@Override
public int compare(student o1, student o2) {
return o1.id.compareTo(o2.id);
}
});
}
else if(c == 2)
{
Arrays.sort(arr, new Comparator<student>() {
@Override
public int compare(student o1, student o2) {
if(!o1.name.equals(o2.name))
{
return o1.name.compareTo(o2.name);
}
else
{
return o1.id.compareTo(o2.id);
}
}
});
}
else
{
Arrays.sort(arr, new Comparator<student>() {
@Override
public int compare(student o1, student o2) {
if(o1.grade != o2.grade)
{
return o1.grade - o2.grade;
}
else
{
return o1.id.compareTo(o2.id);
}
}
});
}
for(int i = 0 ; i < n ;i++)
{
System.out.println(arr[i].id + " " + arr[i].name + " " + arr[i].grade);
}
}
}
class student{
String id ;
String name;
int grade;
public student()
{
}
}