Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:
registration_number final_rank location_number local_rank
The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.
Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
题目大意:
在若干地区分别有若干个学生,将学生的成绩分别在当地和全地区进行排名。
解题思路:
在for循环内定义一个ArrayList对某一地区排名,将排名装入学生属性中,循环外再定义一个ArrayList对全部成绩排名,并装入学生属性中。
注意在for循环内分别向两个list中装学生时,只能new一次,即new出来再依次装。
排名时,注意名次与角标的对应关系。
java代码:(超时)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<Student1025> list = new ArrayList<Student1025>();
for(int i = 1; i <= n;i++) {
List<Student1025> list1 = new ArrayList<Student1025>();
int m = Integer.parseInt(br.readLine());
for(int j = 0; j < m;j++) {
String[] split = br.readLine().split(" ");
String name = split[0];
int score = Integer.parseInt(split[1]);
Student1025 stu = new Student1025(name, score, i);
list1.add(stu);
list.add(stu);
}
Collections.sort(list1);
list1.get(0).rankLocate = 1;
for(int k = 1; k < list1.size();k++) {
if(list1.get(k).score == list1.get(k - 1).score) {
list1.get(k).rankLocate = list1.get(k - 1).rankLocate;
}else {
list1.get(k).rankLocate = k + 1;
}
}
}
Collections.sort(list);
list.get(0).rankFinal = 1;
for(int k = 1; k < list.size();k++) {
if(list.get(k).score == list.get(k - 1).score) {
list.get(k).rankFinal = list.get(k - 1).rankFinal;
}else {
list.get(k).rankFinal = k + 1;
}
}
System.out.println(list.size());
for(Student1025 data : list) {
System.out.println(data);
}
}
}
class Student1025 implements Comparable<Student1025>{
String name;
int score;
int numLocate;
int rankLocate;
int rankFinal;
public Student1025(String name, int score, int numLocate) {
this.name = name;
this.score = score;
this.numLocate = numLocate;
}
@Override
public int compareTo(Student1025 o) {
if(this.score != o.score) {
return o.score - this.score;
}else {
return this.name.compareTo(o.name);
}
}
@Override
public String toString() {
return name + " " + rankFinal + " " + numLocate + " " + rankLocate;
}
}