成绩排序【清华大学】

牛客网题目链接

版本0

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 103;
struct node{
	int no, score;
}E[N];
bool cmp(node a, node b){
	if(a.score != b.score) 
		return a.score < b.score;
	else 
		return a.no < b.no;
}
int main(){
	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		cin>>E[i].no>>E[i].score; 
	}
	sort(E, E+n, cmp);
	for(int i = 0; i < n; i++){
		cout<<E[i].no<<" "<<E[i].score<<endl;
	}
	
	return 0;
}

Java比较器

版本1

外置重载比较器类

import java.util.*;
public class perfomanceSort {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()) {
			int n = in.nextInt();
			String s;
			int age, score;
			Student stu[]= new Student[n];
			for( int i = 0; i < n; i++) {
				s = in.next();
				age = in.nextInt();
				score = in.nextInt();
				stu[i] = new Student(s, age, score);
			}
			Arrays.sort(stu, new cmp());
			for(int i = 0; i < n; i++) {
				System.out.println(stu[i].name+" "+stu[i].age+" "+stu[i].score);
			}
		}
	}
	public static class Student{
		public int age;
		public int score;
		public String name;
		Student(String name, int age, int score){
			this.age = age;
			this.name = name;
			this.score = score;
		}
		}
	public static class cmp implements Comparator<Student>{  

		
		public int compare(Student s1, Student s2) {
			if(s1.score != s2.score) return s1.score < s2.score ? -1: 1; 
			else if(s1.name.compareTo(s2.name) != 0) return s1.name.compareTo(s2.name) < 0 ? -1: 1;
				return s1.age < s2.age ? -1: 1;
		}
	}

}

版本2

内置比较器

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


public class second {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()) {
			int n = in.nextInt();
			Student stu[] = new Student[n];
			for( int i = 0; i < n; i++) {
				stu[i]=new Student(in.next(), in.nextInt(), in.nextInt());
			}
			 Arrays.sort(stu, new Comparator<Student>() {
	         
	             public int compare(Student o1, Student o2) {
	                 if(o1.score == o2.score){
	                   if(o1.name.compareTo(o2.name) == 0){
	                       return o1.age - o2.age;
	                   }
	                   return o1.name.compareTo(o2.name);
	                 }
	                 return o1.score - o2.score;
	             }
	         });
			 
			for(int i = 0; i < n; i++) {
				System.out.println(stu[i].name+" "+stu[i].age+" "+stu[i].score);
			}
		}
	}
	public static class Student{
		int age;
		int score;
		String name;
		Student(String name, int age, int score){
			this.age = age;
			this.name = name;
			this.score = score;
		}
	}
	

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值