Java结构体排序

发福利喽

题目描述

小柒同学最近发工资了,打算拿出其中一部分为自己一直帮助的贫困学校的学习成绩优秀的前 55 名学生作为奖励。期末,每个学生都有 33 门课的成绩:c语言、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按c语言成绩从高到低排序,如果两个同学总分和c语言成绩都相同,那么规定学号小的同学 排在前面。

注意,在前 55 名同学中,每个人的奖品都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分) 是:

77 279279

55 279279

这两行数据的含义是:总分最高的两个同学的学号依次是 77 号、55 号。这两名同学的总分都是 279279 (总分等于输入的c语言、数学、英语三科成绩之和) ,但学号为 77 的学生c语言成绩更高一些。如果你的前两名的输出数据是:

55 279279

77 279279

则按输出错误处理,不能得分。

输入格式

共 n+1n+1行。

第 11 行为一个正整数n ( \le 300)n(≤300),表示该校参加评选的学生人数。

第 22 到 n+1n+1 行,每行有 33 个用空格隔开的数字,每个数字都在 00 到 100100 之间。第 jj 行的 33 个数字依次表示学号为 j-1j−1 的学生的c语言、数学、英语的成绩。每个学生的学号按照输入顺序编号为 1\sim n1∼n

输出格式

共 55 行,每行是两个用空格隔开的正整数,依次表示前 55 名学生的学号和总分。

输入输出样例

输入 #1复制

6

90 67 80

87 66 91

78 89 91

88 99 77

67 89 64

78 89 98

输出 #1复制

6 265

4 264

3 258

2 244

1 237

输入 #2复制

8

80 89 89

88 98 78

90 67 80

87 66 91

78 89 91

88 99 77

67 89 64

78 89 98

输出 #2复制

8 265

2 264

6 264

1 258

5 258

Comparator和Comparable在排序中的应用

java排序是真麻烦

当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序。

一、Comparator

强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collections.sort或Arrays.sort。

接口方法:

/**

* @return o1小于、等于或大于o2,分别返回负整数、零或正整数。

*/

int compare(Object o1, Object o2);

import java.util.*;
import java.io.*;
class student{
    int a;
    int b;
    int c;
    int sum;
    int id;
//    public int compareTo(student s){
//        if(this.sum!=s.sum)return this.sum-s.sum;
//       else if(this.a!=s.a) return this.a-s.a;
//       else  return s.id-this.id;
//
//
//    }

};
class newcompare implements Comparator<student>{

    @Override
    public int compare(student o1, student o2) {
        if(o1.sum!=o2.sum)   return o2.sum-o1.sum;
        else if(o1.a!= o2.a) return o2.a-o1.a;
        else return o1.id-o2.id;
    }
}
public class Main {





    public static void main(String[] args) {



Scanner sc=new Scanner(System.in);
int n= sc.nextInt();
student[] kk=new student[10010];
for(int i=1;i<=n;i++){
    kk[i]=new student();
kk[i].a= sc.nextInt();
kk[i].b= sc.nextInt();
kk[i].c= sc.nextInt();
kk[i].sum= kk[i].a+kk[i].b+kk[i].c;
kk[i].id= i;
}
Arrays.sort(kk,1,n+1,new newcompare());
for(int i=1;i<=5;i++){
    System.out.println(kk[i].id+" "+kk[i].sum);

}




    }



}

二、Comparable

强行对实现它的每个类的对象进行整体排序,实现此接口的对象列表(和数组)可以通过Collections.sort或Arrays.sort进行自动排序。

接口方法:

/**

* @return 该对象小于、等于或大于指定对象o,分别返回负整数、零或正整数。

*/

int compareTo(Object o);

import java.util.*;
import java.io.*;
class student implements Comparable<student>{
    int a;
    int b;
    int c;
    int sum;
    int id;
    public int compareTo(student s){
        if(this.sum!=s.sum)return s.sum-this.sum;
       else if(this.a!=s.a) return s.a-this.a;
       else  return this.id-s.id;


    }

}
//class newcompare implements Comparator<student>{
//
//    @Override
//    public int compare(student o1, student o2) {
//        if(o1.sum!=o2.sum)   return o2.sum-o1.sum;
//        else if(o1.a!= o2.a) return o2.a-o1.a;
//        else return o1.id-o2.id;
//    }
//}
public class Main {





    public static void main(String[] args) {



Scanner sc=new Scanner(System.in);
int n= sc.nextInt();
student[] kk=new student[10010];
for(int i=1;i<=n;i++){
    kk[i]=new student();
kk[i].a= sc.nextInt();
kk[i].b= sc.nextInt();
kk[i].c= sc.nextInt();
kk[i].sum= kk[i].a+kk[i].b+kk[i].c;
kk[i].id= i;
}
Arrays.sort(kk,1,n+1);
for(int i=1;i<=5;i++){
    System.out.println(kk[i].id+" "+kk[i].sum);

}


    }



}

降序排序

import java.util.*;
import java.io.*;
//class student implements Comparable<student>{
//    int a;
//    int b;
//    int c;
//    int sum;
//    int id;
//    public int compareTo(student s){
//        if(this.sum!=s.sum)return s.sum-this.sum;
//       else if(this.a!=s.a) return s.a-this.a;
//       else  return this.id-s.id;
//
//
//    }
//
//}
class newcompare implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}
public class Main {





    public static void main(String[] args) {



Scanner sc=new Scanner(System.in);
Integer n= sc.nextInt();
Integer []a=new Integer[100];
for(int i=0;i<n;i++){
    a[i]= sc.nextInt();
}
Arrays.sort(a,0,n,new  newcompare());
        for(int i=0;i<n;i++){
            System.out.println(a[i]+" ");
        }

    }



}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怀化第一深情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值