(HDOJ)What Is Your Grade-java实现

原题

http://acm.hdu.edu.cn/game/entry/status/index.php

  • 本次开发遇到的坑点,在于有分数加成的是总答题数人的前一半,不是第一个,而且只有一个人答出没有分数加成。
  • 不能使用TreeSet 直接排好,因为TreeMap是按key排序的,所以TreeSet中的元素也是排好序的。显然元素在插入TreeSet时compareTo()方法要被调用,所以TreeSet中的元素要实现Comparable接口。TreeSet作为一种Set,它不允许出现重复元素。TreeSet是用compareTo()来判断重复元素的,而非equals()。

源码


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num = in.nextInt();
            //各答题数的总人数
            int[] tatol = {0,0,0,0};
            //达标人数计数
            int[] count = {0,0,0,0};
            in.nextLine();
            if(num < 0) return;
            
            int[] resultArray = new int[num];
            List<Student> students = new ArrayList<>(num);
            for(int i = 0;i < num;i++){
                Student student = new Student();
                student.id = i;
                String[] line = in.nextLine().split("\\s");
                student.solved = Integer.parseInt(line[0]);
                if(student.solved < 5 && student.solved > 0)
                    tatol[student.solved - 1]++;
                student.time = line[1];
                students.add(student);
            }
            
            students.sort(new StuComparetor());
            Iterator<Student> it = students.iterator();
            while (it.hasNext()){
                Student temp = it.next();
                switch (temp.solved) {
                    case 0:
                        temp.score = 50;
                        break;
                    case 1:
                        count[0]++;
                        if (count[0] <= tatol[0] / 2) {
                            temp.score = 65;
                        } else {
                            temp.score = 60;
                        }
                        break;
                    case 2:
                        count[1]++;
                        if (count[1] <= tatol[1] / 2) {
                            temp.score = 75;
                        } else {
                            temp.score = 70;
                        }
                        break;
                    case 3:
                        count[2]++;
                        if (count[2] <= tatol[2] / 2) {
                            temp.score = 85;
                        } else {
                            temp.score = 80;
                        }
                        break;
                    case 4:
                        count[3]++;
                        if (count[3] <= tatol[3] / 2) {
                            temp.score = 95;
                        } else {
                            temp.score = 90;
                        }
                        break;
                    case 5:
                        temp.score = 100;
                        break;
                }
                //按输入顺序输出成绩
                resultArray[temp.id] = temp.score;
            }

            for (int score : resultArray){
                System.out.println(score);
            }
            System.out.println();
        }
    }

    static class Student{
        int id;
        int solved;
        String time;
        int score;
    }

    static class StuComparetor implements Comparator<Student>{
        int result;
        @Override
        public int compare(Student o1, Student o2) {
            result = Integer.compare(o2.solved, o1.solved);
            if(result == 0)
                result = o1.time.compareTo(o2.time);

            return result;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值