某校2019专硕编程题

问题

输入学生成绩,并按输入顺序编号,再按成绩进行降序排序,输出前10名的学生成绩,如果学生人数不足十人则只输出仅有的学生成绩。
例如 输入20 30 40 50 70 -1(结束),输出:X号,XX分

Java实现

static class Student{
        Integer num,score;
        Student(Integer num,Integer score){
            this.num = num;
            this.score = score;
        }
    }

public static void test05(){
        Scanner sc = new Scanner(System.in);
        List<Student> list = new ArrayList<>();
        int n = 1;
        while (true){
            int score = sc.nextInt();
            if (score == -1) break;
            list.add(new Student(n++,score));
        }
        int length = list.size();
        while (true){
            for (int i = 0; i < length-1; i++) {
                if (list.get(i).score < list.get(i+1).score){
                    Student swap = list.get(i);
                    list.set(i,list.get(i+1));
                    list.set(i+1,swap);
                }
            }
            length--;
            if (length == 0) break;
        }
        Iterator<Student> it = list.iterator();
        while (it.hasNext()){
            if (length > 9) break;
            Student student = it.next();
            System.out.println(student.num+"号 "+student.score+"分");
            length++;
        }
}

你可能感兴趣的:(#,算法之美,专硕编程题)