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;
        }
    }


}

版本3

  • 2021.04.03

  • 主要考点:数组排序,重写Comparator方法

    import java.util.*;
    public class Main{
    
      public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          int n = in.nextInt();
          String name;
          int age, score;
          List<Stu> list = new ArrayList();
          for(int i = 0; i < n; i++){
                  name = in.next();
                  age = in.nextInt();
                  score = in.nextInt();
                  list.add(new Stu(name, age,score));
          }
          // 排序
          Collections.sort(list, (o1, o2) -> {
              if(o1.score != o2.score) return o1.score - o2.score;
              else if(!o1.name.equals(o2.name)) return o1.name.compareTo(o2.name);
              else return o1.age - o2.age;
          });
    
          // 输出
          for(Stu s: list){
              System.out.println(s.name+" "+s.age +" "+s.score);
          }
      }
      public static class Stu{
          String name;
          int age;
          int score;
    
          public Stu(String name, int age, int score) {
              this.name = name;
              this.age = age;
              this.score = score;
          }
      }
    }
全部评论

相关推荐

10-29 15:51
嘉应学院 Java
后端转测开第一人:你把简历的学历改成北京交通大学 去海投1000份发现基本还是没面试
点赞 评论 收藏
分享
想干测开的tomca...:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务