ZZULIOJ 1197: 考试排名(一)(结构体专题),Java
题目描述
今天浙大研究生复试的上机考试跟传统笔试的打分规则相似,总共有n道题,每道题有对应分值,最后计算总成绩。现给定录取分数线,请你写程序找出最后通过分数线的考生,并将他们的成绩按降序打印。
输入
第1行给出考生人数N ( 1<= N<=100 )、考题数M (1<=M<=10 )、分数线(正整数)G;
第2行排序给出第1题至第M题的正整数分值;
以下N行,每行给出一名考生的准考证号(长度不超过20的字符串)、该生解决的题目总数m、以及这m道题的题号
(题目号由1到M)。
输出
首先在第1行输出不低于分数线的考生人数n,随后n行按分数从高到低输出上线考生的考号与分数,其间用1空格分隔。若有多名考生分数相同,则按他们考号的升序输出。
样例输入 Copy
4 5 25
10 10 12 13 15
CS004 3 5 1 3
CS003 5 2 4 1 3 5
CS002 2 1 2
CS001 3 2 3 5
样例输出 Copy
3
CS003 60
CS001 37
CS004 37
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Student {
String id;
int totalScore;
public Student(String id, int totalScore) {
this.id = id;
this.totalScore = totalScore;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // Number of students
int m = sc.nextInt(); // Number of questions
int g = sc.nextInt(); // Pass score
int[] scores = new int[m];
for (int i = 0; i < m; i++) {
scores[i] = sc.nextInt();
}
ArrayList<Student> students = new ArrayList<>();
for (int i = 0; i < n; i++) {
String id = sc.next();
int solvedCount = sc.nextInt();
int totalScore = 0;
for (int j = 0; j < solvedCount; j++) {
int questionIndex = sc.nextInt() - 1;
totalScore += scores[questionIndex];
}
if (totalScore >= g) {
students.add(new Student(id, totalScore));
}
}
Collections.sort(students, (s1, s2) -> {
if (s1.totalScore != s2.totalScore) {
return Integer.compare(s2.totalScore, s1.totalScore);
} else {
return s1.id.compareTo(s2.id);
}
});
System.out.println(students.size());
for (Student student : students) {
System.out.println(student.id + " " + student.totalScore);
}
}
}