解题思路:
这道题有一个注意事项:
*(you can get the former(前者) only when your rank is in the first half of all students who solve 4 problems).
意思是:例:这个答对4个问题的学生必须是占全部答对4个问题的学生的一半及一半以上,才是95,其他都是90分*
1.创建两个数组,一个记录解决了(1,2,3,4)个问题的学生个数;一个为后期便于计算这个同学是得(95或者90)分;
2.创建一个学生类:定义成员变量:
id(记录该学生是输入的第几个学生)
score(记录该学生的的分数)
time(记录该学生所用的时间)
solved(记录该学生解决问题个数)
3.创建一个ArrayList集合,保存各个学生
4.创建一个数组,用于按照学生id输出学生成绩
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
static String begin = "00:00:00";
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
while(!sc.hasNext("-1")){
int menber = sc.nextInt();
int[] total = {
0,0,0,0};
int[] count = {
0,0,0,0};
List<student> ans = new ArrayList<>(menber);
int[] resultArr = new int[menber];
for(int i = 0; i < menber; i++){
student stu = new student();
stu.id = i;
stu.solved = sc.nextInt();
stu.time = GetTime(sc.next());
if(stu.solved < 5 && stu.solved > 0){
total[stu.solved-1]++;
}
ans.add(stu);
}
ans.sort(new compareStu());
Iterator<student> it = ans.iterator();
while(it.hasNext()){
student temp = it.next();
switch (temp.solved){
case 0:
temp.score = 50;
break;
case 1:
count[0]++;
if(count[0] <= total[0] / 2){
temp.score = 65;
}else{
temp.score = 60;
}
break;
case 2:
count[1]++;
if(count[1] <= total[1] / 2){
temp.score = 75;
}else{
temp.score = 70;
}
break;
case 3:
count[2]++;
if(count[2] <= total[2] / 2){
temp.score = 85;
}else{
temp.score = 80;
}
break;
case 4:
count[3]++;
if(count[3] <= total[3] / 2){
temp.score = 95;
}else{
temp.score = 90;
}
break;
case 5:
temp.score = 100;
break;
}
resultArr[temp.id] = temp.score;
}
for(int score : resultArr){
System.out.println(score);
}
System.out.println();
}
}
private static long GetTime(String next) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
long MyTime = sdf.parse(begin).getTime() - sdf.parse(next).getTime();
return MyTime;
}
static class student{
int id;
int solved;
long time;
int score;
}
static class compareStu implements Comparator<student>{
int result;
@Override
public int compare(student o1, student o2) {
result = Integer.compare(o2.solved, o1.solved);
if(result == 0)
result = Long.compare(o2.time,o1.time);
return result;
}
}
}