编程语言
首页 > 编程语言> > 【创建Comparator对象规定排序秩序】【Java】【P1093 】

【创建Comparator对象规定排序秩序】【Java】【P1093 】

作者:互联网

package Package;

import java.io.*;
import java.util.*;

class Student{
	int ID;
	int ch;
	int ma;
	int en;
	int sum;
	public Student(int ID, int ch, int ma, int en) {
		this.ID = ID;
		this.ch = ch;
		this.ma = ma;
		this.en = en;
		this.sum=ch+ma+en;
	}
}

public class Main{

    public static void main(String[] args) throws IOException{
    	
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    	PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		
		in.nextToken();
    	int N = (int)in.nval;
    	
    	Student[] students=new Student[N];
		for(int i=0;i<N;i++) {
			in.nextToken();
			int ch=(int)in.nval;
			in.nextToken();
			int ma=(int)in.nval;
			in.nextToken();
			int en=(int)in.nval;
			students[i]=new Student(i+1,ch,ma,en);
		}
		
		Comparator<Student> comp = new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				if(o1.sum>o2.sum) {
					return -1;
				}else if(o1.sum==o2.sum) {
					if(o1.ch>o2.ch) {
						return -1;
					}else if(o1.ch==o2.ch) {
						if(o1.ID<o2.ID) {
							return -1;
						}else {
							return 1;
						}
					}else {
						return 1;
					}
				}
				return 1;
			}
		};
		
		Arrays.sort(students,comp);
		for(int i=0;i<5;i++) {
			out.println(students[i].ID+" "+students[i].sum);
		}
    
		out.flush();
		out.close();
    }
}

标签:ch,Java,Student,Comparator,int,sum,en,P1093,new
来源: https://blog.csdn.net/m0_57937908/article/details/122766099