PAT A 1012 (Java实现)(简便且附详细注解)The Best Rank

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:57   2174   0

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 C, M, E 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 C, M 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

代码如下:

package patA;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class B1012 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  int number = sc.nextInt();// 输入记录的人数
  int check = sc.nextInt();// 要求查询的人数

  char course[] = { 'A', 'C', 'M', 'E' };// 按优先级顺序排序

  int rank[][] = new int[10000000][4];// 排名数组

  Student2 stu[] = new Student2[number];// 学生数组

  // 对数组中的元素初始化
  for (int i = 0; i < number; i++) {
   Student2 student = new Student2();
   student.id = sc.nextInt();
   for (int j = 1; j < 4; j++) {
    student.grade[j] = sc.nextInt();

   }
   student.grade[0] = (student.grade[1] + student.grade[2] + student.grade[3]) / 3;
   stu[i] = student;// 初始化好了以后,放到数组中

  }

  int now;// now用来记录按照什么分来对stu[] 数组排序,
  // 按now号对stu[]数组进行排序
  Comparator2 mycomparator = new Comparator2();
  for (now = 0; now < 4; now++) {
   // Comparator2 mn=new Comparator2(i);
   mycomparator.now = now;
   Arrays.sort(stu, mycomparator);// 对所有学生按照该分数排序

   rank[stu[0].id][now] = 1;// 排序完后,将分数最高的设为1
   for (int a = 1; a < number; a++) {
    if (stu[a].grade[now] == stu[a - 1].grade[now]) {
     rank[stu[a].id][now] = rank[stu[a - 1].id][now];

    } else {
     rank[stu[a].id][now] = a + 1;

    }
   }

  }

  // 先将要查询的id 都读取到一个数组中存储
  int chaxun[] = new int[check];
  for (int y = 0; y < check; y++) {
   chaxun[y] = sc.nextInt();
  }

  // 查询
  for (int i = 0; i < check; i++) {
   int idi = chaxun[i];
   if (rank[idi][0] == 0) {
    // 该考生不存在
    System.out.println("N/A");
   } else {
    int k = 0;
    for (int t = 0; t < 4; t++) {
     if (rank[idi][t] > rank[idi][k]) {
      k = t;
     }
    }

    System.out.println(rank[idi][k] + " " + course[k]);
   }
  }

 }

}

class Student2 {
 int id;
 int grade[] = new int[4];

 // 构造函数初始化
 /*
  * public Student2(int id,int grade[]) { this.id=id; this.grade=grade; }
  */
}

// 比较器
class Comparator2 implements Comparator<Student2> {
 int now;

 public int compare(Student2 stu1, Student2 stu2) {

  if (stu1.grade[now] > stu2.grade[now]) {
   return -1;
  } else if (stu1.grade[now] < stu2.grade[now]) {
   return 1;
  } else {
   return 0;
  }

 }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP