Java教程

1012 The Best Rank (25 分) java 题解

本文主要是介绍1012 The Best Rank (25 分) java 题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of CME and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of CM and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

解题思路:

题目大意:给定一些学生和他们的各科成绩,输出指定学生的最好名次(一样时按权重)。

读入学生的信息后,将学生的姓名key,对象value装入map,同时装入list中,map中方便通过姓名查询信息,list方便排序,由于需要对各科成绩进行排序,需要用到Comparator比较器,排完序后,要及时修正成绩相同但名次不同的情况,改完将各科名次装入对象的属性中。挑选最好属性时,可以对各科名次排序,最后挑选最好名次所对应的学科。

代码在对各科成绩排序时过于冗余,也可以将各科成绩分别装入list中,直接排序,查找每个学生时,分别查找该学生的个课的成绩名次,最后比较名次,可以减少代码量。

输出时,应该讲所输出对的内容拼接到StringBuilder中,可减少耗时。

储备知识:

 java中对自己定义的类进行按某一属性进行排序时,需要用到两个接口:Comparable<T>,Comparator<T>。

 Comparable接口:自然排序,接口中定义了一个compareTo的抽象方法,继承该接口时,需要重写该方法。常用于只对类中的某一个规则进行排序。

class T implements Comparable<T>{

 @Override
	public int compareTo(T o) {
		// TODO Auto-generated method stub
		return 0;
	}

}

Comparator接口:定制排序。常用于对类中的多个规则进行排序。

Collections.sort(list, new Comparator<T>() {
			@Override
			public int compare(T o1, T o2) {
				
			}
});

java代码:

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		int n = Integer.parseInt(split[0]);
		int nn = Integer.parseInt(split[1]);
		List<Student1012> list = new ArrayList<>();
		Map<String,Student1012> map = new HashMap<>();
		for(int i = 0; i < n;i++) {
			split = br.readLine().split(" ");
			String name = split[0];
			int c = Integer.parseInt(split[1]);
			int m = Integer.parseInt(split[2]);
			int e = Integer.parseInt(split[3]);
			Student1012 stu = new Student1012(name, c, m, e);
			list.add(stu);
			map.put(name, stu);
		}
		
		Collections.sort(list, new Comparator<Student1012>() {
			@Override
			public int compare(Student1012 o1, Student1012 o2) {
				return o2.c - o1.c;
			}
		});
		list.get(0).rankC = 1;
		for(int i = 1; i < list.size();i++) {
			if(list.get(i).c == list.get(i - 1).c) {
				list.get(i).rankC = list.get(i - 1).rankC;
			}else {
				list.get(i).rankC = i + 1;
			}
		}
		
		Collections.sort(list, new Comparator<Student1012>() {
			@Override
			public int compare(Student1012 o1, Student1012 o2) {
				return o2.m - o1.m;
			}
		});
		list.get(0).rankM = 1;
		for(int i = 1; i < list.size();i++) {
			if(list.get(i).m == list.get(i - 1).m) {
				list.get(i).rankM = list.get(i - 1).rankM;
			}else {
				list.get(i).rankM = i + 1;
			}
		}
		
		Collections.sort(list, new Comparator<Student1012>() {
			@Override
			public int compare(Student1012 o1, Student1012 o2) {
				return o2.e - o1.e;
			}
		});
		list.get(0).rankE = 1;
		for(int i = 1; i < list.size();i++) {
			if(list.get(i).e == list.get(i - 1).e) {
				list.get(i).rankE = list.get(i - 1).rankE;
			}else {
				list.get(i).rankE = i + 1;
			}
		}
		
		Collections.sort(list, new Comparator<Student1012>() {
			@Override
			public int compare(Student1012 o1, Student1012 o2) {
				return o2.a - o1.a;
			}
		});
		list.get(0).rankA = 1;
		for(int i = 1; i < list.size();i++) {
			if(list.get(i).a == list.get(i - 1).a) {
				list.get(i).rankA = list.get(i - 1).rankA;
			}else {
				list.get(i).rankA = i + 1;
			}
		}
		List<String> list2 = new ArrayList<String>();
		
		for(int i = 0; i < n;i++) {
			list.get(i).selectBest();
		}
		
		for(int i = 0; i < nn;i++) {
			list2.add(br.readLine());
		}
		StringBuilder builder = new StringBuilder();
		for(int i = 0; i < nn;i++) {
			Student1012 value = map.get(list2.get(i));
			if(value != null) {
				builder.append(value);
			}else {
				builder.append("N/A\n");
			}
		}
		System.out.println(builder.toString().trim());
	}
}
class Student1012{
	String name;
	int c;
	int m;
	int e;
	int a;
	int rankA;
	int rankC;
	int rankM;
	int rankE;
	int bestRank;
	char bestSubject;
	
	public Student1012(String name, int c, int m, int e) {
		this.name = name;
		this.c = c;
		this.m = m;
		this.e = e;
		a = (c + m + e) / 3;
	}
	
	public void selectBest() {
		int []arr = new int[4];
		arr[0] = rankA;
		arr[1] = rankC;
		arr[2] = rankM;
		arr[3] = rankE;
		Arrays.sort(arr);
		bestRank = arr[0];
		if(bestRank == rankA) {
			bestSubject = 'A';
		}else if(bestRank == rankC) {
			bestSubject = 'C';
		}else if(bestRank == rankM) {
			bestSubject = 'M';
		}else {
			bestSubject = 'E';
		}
	}

	@Override
	public String toString() {
		return bestRank + " " + bestSubject + "\n";
	}
}

PTA提交截图:

 

这篇关于1012 The Best Rank (25 分) java 题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!