java,sort函数的深刻理解
先来看看题目吧:
链接:https://www.nowcoder.com/questionTerminal/97b6a49a85944650b2e3d0660b91c324
来源:牛客网
- 热度指数:16728
时间限制:1秒
空间限制:32768K - 算法知识视频讲解
小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”
现给出一批考生的德才分数,请根据司马光的理论给出录取排名。
输入描述:
输入第1行给出3个正整数,分别为:N(<=10
5
),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格 被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到 但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼 亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。 随后N行,每行给出一位考生的信息,包括:准考证号、德分、才分,其中准考证号为8位整数,德才分为区间[0, 100]内的整数。数字间以空格分隔。
输出描述:
输出第1行首先给出达到最低分数线的考生人数M,随后M行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人 总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。
输入
14 60 80 10000001 64 90 10000002 90 60 10000011 85 80 10000003 85 80 10000004 80 85 10000005 82 77 10000006 83 76 10000007 90 78 10000008 75 79 10000009 59 90 10000010 88 45 10000012 80 100 10000013 90 99 10000014 66 60
输出
12 10000013 90 99 10000012 80 100 10000003 85 80 10000011 85 80 10000004 80 85 10000007 90 78 10000006 83 76 10000005 82 77 10000002 90 60 10000014 66 60 10000008 75 79 10000001 64 90
代码实现如下:
import java.util.Arrays;
import java.util.Scanner;
import java.util.Comparator; class A
{
long num;
int character;
int talent;
int grade;
}
class cmp implements Comparator<A>
{
public int compare(A a,A b)
{
if(a.grade < b.grade)
{
return 1;
}
else if(a.grade == b.grade)
{
if(a.character < b.character)
{
return 1;
}
else if(a.character == b.character)
{
if(a.num > b.num)
{
return 1;
}
else if(a.num < b.num)
{
return -1;
}
else
{
return 0;
}
}
else
{
return -1;
}
}
else
{
return -1;
}
}
}
public class Main
{
public static final int MAX = 100005;
public static A str1[] = new A[MAX];
public static A str2[] = new A[MAX];
public static A str3[] = new A[MAX];
public static A str4[] = new A[MAX];
public static void main(String[] args)
{
int N,L,H;
Scanner cin = new Scanner(System.in);
N = cin.nextInt();
L = cin.nextInt();
H = cin.nextInt();
int a,b,c;
a = b = c = 0;
int len[] = new int[5];
int num = 0;
for(int i = 0 ; i < N ; i++)
{
a = cin.nextInt();
b = cin.nextInt();
c = cin.nextInt();
if(b >= H && c >= H)
{
str1[len[1]] = new A();
str1[len[1]].num = a;
str1[len[1]].character = b;
str1[len[1]].talent = c;
str1[len[1]].grade = b+c;
len[1]++;
num++;
}
else if(b >= H && c >= L)
{
str2[len[2]] = new A();
str2[len[2]] = new A();
str2[len[2]].num = a;
str2[len[2]].character = b;
str2[len[2]].talent = c;
str2[len[2]].grade= b+c;;
len[2]++;
num++;
}
else if(b >= L && c >= L && b >= c)
{
str3[len[3]] = new A();
str3[len[3]].num = a;
str3[len[3]].character = b;
str3[len[3]].talent = c;
str3[len[3]].grade = c+b;
len[3]++;
num++;
}
else if(b >= L && c >= L)
{
str4[len[4]] = new A();
str4[len[4]].num = a;
str4[len[4]].character = b;
str4[len[4]].talent = c;
str4[len[4]].grade = c+b;
len[4]++;
num++;
}
}
Arrays.sort(str1,0,len[1],new cmp());
Arrays.sort(str2,0,len[2],new cmp());
Arrays.sort(str3,0,len[3],new cmp());
Arrays.sort(str4,0,len[4],new cmp());
System.out.println(num);
for(int i = 0 ; i < len[1] ; i++)
{
System.out.println(str1[i].num + " " + str1[i].character + " " + str1[i].talent);
}
for(int i = 0 ; i < len[2] ; i++)
{
System.out.println(str2[i].num + " " + str2[i].character + " " + str2[i].talent);
}
for(int i = 0 ; i < len[3] ; i++)
{
System.out.println(str3[i].num + " " + str3[i].character + " " + str3[i].talent);
}
for(int i = 0 ; i < len[4] ; i++)
{
System.out.println(str4[i].num + " " + str4[i].character + " " + str4[i].talent);
}
}
}
java,sort函数的深刻理解的更多相关文章
- Java Integer 与 int 深刻理解
今天在做Object 自动转为Integer 类型之后的判断,遇到一个不理解的点,当数值超过127之后,两个数值相同的Object 对象用 == 判断的结果是false. Object a = 128 ...
- Java基础 带你深刻理解自动装箱,拆箱含义
1.什么是装箱,什么是拆箱装箱:把基本数据类型转换为包装类.拆箱:把包装类转换为基本数据类型.基本数据类型所对应的包装类:int(几个字节4)- Integerbyte(1)- Byteshort(2 ...
- javascript 面向对象程序设计--深刻理解对象
javascript中,每个对象都是基于一个引用类型创建的,我们可以把ECMAScript 的对象想象成散列表:无非就是一组名值对,其中值可以是数据或函数. 深刻理解对象 创建自定义对象的最简单方式就 ...
- 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因
声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...
- 深刻理解Java中的String、StringBuffer和StringBuilder的差别
声明:本博客为原创博客,未经同意.不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(链接为http://blog.csdn.net/bettarwang/article/detai ...
- 别翻了,这篇文章绝对让你深刻理解java类的加载以及ClassLoader源码分析【JVM篇二】
目录 1.什么是类的加载(类初始化) 2.类的生命周期 3.接口的加载过程 4.解开开篇的面试题 5.理解首次主动使用 6.类加载器 7.关于命名空间 8.JVM类加载机制 9.双亲委派模型 10.C ...
- python 中的sort 和java中的Collections.sort()函数的使用
x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...
- 理解sort()函数的排序原理
看了很多关于sort()函数的定义和解释还是不太清楚,尤其是初学者很容易看懵,这里讲讲自己是如何理解的. 首先,要理解sort()内部是利用递归进行冒泡排序的: 例如: var arr = [1, 5 ...
- java中的Sort函数,你值得看
基于C语言中的sort如此这么方便,自然而然,java中也有类似C的sort函数. 1.普通数组:Arrays.sort(数组名,开始位置,结束位置). 2.类中属性排序: 模板: class A { ...
随机推荐
- topcoder srm 707 div1
1 构造一个棋盘,长宽n,m不超过50,每个格子为障碍或者非障碍两种,使得从(0,0)到(n-1,m-1)的最短路为给定的值k. 思路:如果k小于等于98,那么一定存在没有障碍的棋盘满足要求.否则,最 ...
- javaEE体系结构【转载】
转载自: http://blog.csdn.net/chjskarl/article/details/72629014?locationNum=3&fps=1 JavaEE是一套使用Java进 ...
- 最后一次谈 VirtualBox的安装方法
用 VirtualBox....run 或 .rpm安装都可以, 最重要的是要 用 /usr/sbin/vboxconfig -> vboxdrv.sh --> 去创建 VirutalBo ...
- LOJ#2452. 「POI2010」反对称 Antisymmetry
题目描述 对于一个 \(0/1\) 字符串,如果将这个字符串 \(0\) 和 \(1\) 取反后,再将整个串反过来和原串一样,就称作「反对称」字符串.比如 \(00001111\) 和 \(01010 ...
- springboot配置redis
https://www.cnblogs.com/xiaoping1993/p/7761123.html https://www.cnblogs.com/gdpuzxs/p/7222309.html s ...
- 论文笔记:Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments
Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments 2017-10-25 16:38:23 [Proj ...
- Perceptual Losses for Real-Time Style Transfer and Super-Resolution and Super-Resolution 论文笔记
Perceptual Losses for Real-Time Style Transfer and Super-Resolution and Super-Resolution 论文笔记 ECCV 2 ...
- Wijmo 2017路线图
2016年是Wijmo团队发展和增长的另一个富有成效的一年.回顾我们2016年的路线图,您可以看到我们交付了我们承诺的一切.让我们回顾一下2016年的亮点: 我们第一个全面支持Angular 2 互操 ...
- win10 右键菜单很慢的解决方式
本来想用 win7 的,不想花很多时间折腾了.现在新电脑主板硬盘CPU都在排挤 win7 ,真是可怜呀.正题: 新电脑的性能应该还算不错的, 18 年跑分 29w 以上,但在图标上面右键却都要转圈几秒 ...
- windows 上让文件类型和程序关联的批处理程序。
文件关联工具 地址: https://github.com/wll8/assoc-tool 本工具可以用来为你的便携程序添加文件关联,比如 nodepad2.exe . vscode 或其他图片处理程 ...