PAT甲级练习1012. The Best Rank (25)

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

我的思路是,学生类记录各门成绩以及平均成绩,各门的排名。用ArrayList去保存学生,用外部对象比较器对四种成绩进行排序,最后按照A>C>M>E的方式去输出最高的排名。 最后两个点总是过不去,求大神指教





import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * @author 雨瓣
 * @date 2019/9/17 11:07
 */
public class new1012 {
    static ArrayList<student> students = new ArrayList<>();
    static ArrayList<Integer> studentsId = new ArrayList<>();
    //排序方式1
    static class sort1 implements Comparator<student> {
        
        @Override
        public int compare(student o1, student o2) {
            if(o1.getC()<o2.getC()){
                return 1;
            }else{
                return -1;
            }
        }
    }

    //排序方式2
    static class sort2 implements Comparator<student> {

        @Override
        public int compare(student o1, student o2) {
            if(o1.getM()<o2.getM()){
                return 1;
            }else{
                return -1;
            }
        }
    }

    //排序方式3
    static class sort3 implements Comparator<student> {

        @Override
        public int compare(student o1, student o2) {
            if(o1.getE()<o2.getE()){
                return 1;
            }else{
                return -1;
            }
        }
    }

    //排序方式4
    static class sort4 implements Comparator<student> {

        @Override
        public int compare(student o1, student o2) {
            if(o1.getAvg()<o2.getAvg()){
                return 1;
            }else{
                return -1;
            }
        }
    }

    //排序方式5
    static class sort5 implements Comparator<student> {

        @Override
        public int compare(student o1, student o2) {
            if(o1.getId()>o2.getId()){
                return 1;
            }else{
                return -1;
            }
        }
    }

    static class sortId implements Comparator<Integer>{

        @Override
        public int compare(Integer o1, Integer o2) {
            if(o1>o2){
                return 1;
            }else{
                return -1;
            }
        }
    }

    public static class student  {
        private int Id;
        private double C;
        private double M;
        private double E;
        private double avg;
        private int Crank;
        private int Mrank;
        private int Erank;
        private int Avgrank;
        private String best;

        public String getBest() {
            return best;
        }

        public void setBest() {
            int max = this.Avgrank;
            this.best = this.Avgrank + " A" ;
            if (this.Crank < max){
                this.best = this.Crank + " C";
                max = this.Crank;
            }
            if (this.Mrank < max) {
                this.best = this.Mrank + " M";
                max = this.Mrank;
            }
            if (this.Erank < max){
                this.best = this.Erank + " E";
                max = this.Erank;
            }

            System.out.println(this.best);
        }
        public student(int id, double c, double m, double e) {
            this.Id = id;
            this.C = c;
            this.M = m;
            this.E = e;
            this.avg =( c + m +e );
        }

        public int getAvgrank() {
            return Avgrank;
        }

        public void setAvgrank(int avgrank) {
            Avgrank = avgrank;
        }

        public int getCrank() {
            return Crank;
        }

        public void setCrank(int crank) {
            Crank = crank;
        }

        public int getMrank() {
            return Mrank;
        }

        public void setMrank(int mrank) {
            Mrank = mrank;
        }

        public int getErank() {
            return Erank;
        }

        public void setErank(int erank) {
            Erank = erank;
        }

        public int getId() {
            return Id;
        }

        public void setId(int id) {
            Id = id;
        }

        public double getC() {
            return C;
        }

        public void setC(int c) {
            C = c;
        }

        public double getM() {
            return M;
        }

        public void setM(int m) {
            M = m;
        }

        public double getE() {
            return E;
        }

        public void setE(int e) {
            E = e;
        }

        public double getAvg() {
            return avg;
        }

        public void setAvg(int avg) {
            this.avg = avg;
        }


    }




    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String key[] = bf.readLine().split(" ");
        for(int j=0;j<Integer.parseInt(key[0]);j++){
            String datas[] = bf.readLine().split(" ");
            double []nweDatas = new double[datas.length-1];
            int studentId = Integer.parseInt(datas[0]);
            studentsId.add(studentId);
            for (int i=1;i<datas.length;i++) {
                nweDatas[i-1]= Double.parseDouble(datas[i]);
            }
            students.add(new student(studentId,nweDatas[0],nweDatas[1],nweDatas[2]));
        }

        Collections.sort(students,new sort1());
        for(int i=0;i<students.size();i++){
            students.get(i).setCrank(i+1);
            if(i>0 && students.get(i).getC()==students.get(i-1).getC()){
                students.get(i).setCrank(students.get(i-1).getCrank());//如果成绩一样,排名也一样
            }
        }
        Collections.sort(students,new sort2());
        for(int i=0;i<students.size();i++){
            students.get(i).setMrank(i+1);
            if(i>0 && students.get(i).getM()==students.get(i-1).getM()){
                students.get(i).setMrank(students.get(i-1).getMrank());//如果成绩一样,排名也一样
            }
        }
        Collections.sort(students,new sort3());
        for(int i=0;i<students.size();i++){
            students.get(i).setErank(i+1);
            if(i>0 && students.get(i).getE()==students.get(i-1).getE()){
                students.get(i).setErank(students.get(i-1).getErank());//如果成绩一样,排名也一样
            }

        }
        Collections.sort(students,new sort4());
        for(int i=0;i<students.size();i++){
            students.get(i).setAvgrank(i+1);
            if(i>0 && students.get(i).getAvg()==students.get(i-1).getAvg()){
                students.get(i).setAvgrank(students.get(i-1).getAvgrank());//如果成绩一样,排名也一样
            }
//            System.out.println(students.get(i).getId()+"\t"+students.get(i).getCrank()  +"\t"+students.get(i).getMrank() +"\t"+students.get(i).getErank()+"\t"+students.get(i).getAvgrank());

        }
        Collections.sort(students,new sort5());
        Collections.sort(studentsId,new sortId());
//        for(int i=0;i<students.size();i++){
//            System.out.println(students.get(i).getId()+"\t"+students.get(i).getCrank()  +"\t"+students.get(i).getMrank() +"\t"+students.get(i).getErank()+"\t"+students.get(i).getAvgrank());
//
//        }
        for(int j=0;j<Integer.parseInt(key[1]);j++){
            int newId= Integer.parseInt(bf.readLine());
            int id = studentsId.indexOf(newId);
            if(id==-1){
                System.out.println("N/A");
            }else{
                    students.get(id).setBest();
            }

        }

        bf.close();
    }


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

本版积分规则

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

下载期权论坛手机APP