1015 德才论 (25分)
原题链接:传送门
一、题目


二、解析
思路
题目读懂,新建一个学生类用于存放值,顺便写3个方法。然后排序一下,最主要难在排序。
这题Java过不了,用其他语言过吧。

代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* @题目 1015 德才论 (25分)
*
* @描述 把题目读懂。
* 第一类:H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序
* 第二类:才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;
* 第三类:德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;
* 第四类:其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。
* 第五类就不管他了。
* 这题难在最后的排序上:当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。
*
* @author ChangSheng
* @date 2020-02-16
*/
public class Main {
public static void main(String[] args) throws IOException {
int N = Reader.nextInt(), L = Reader.nextInt(), H = Reader.nextInt();
// 存放分数高于L的学生
List<Student> students = new ArrayList<>();
for (int i = 0; i < N; i++) {
// 当前学生准考证号、德分、才分
int id = Reader.nextInt(), de = Reader.nextInt(), cai = Reader.nextInt();
if (de >= L && cai >= L) {
students.add(new Student(id, de, cai));
}
}
// 排序
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 分别比较类别,总分,德分,准考证
if (s1.getClasses(H) == s2.getClasses(H)) {
if (s1.getSum() == s2.getSum()) {
if (s1.de == s2.de) {
return s1.id - s2.id;
}
return s2.de - s1.de;
}
return s2.getSum() - s1.getSum();
}
return s1.getClasses(H) - s2.getClasses(H);
}
});
System.out.println(students.size());
for (Student s : students) {
System.out.println(s);
}
}
}
/** 快速输入类 */
class Reader {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
/** 获取下一段文本 */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
/** 学生类 */
class Student {
/** 准考证、德分、才分 */
int id, de, cai;
public Student(int id, int de, int cai) {
this.id = id;
this.de = de;
this.cai = cai;
}
@Override
public String toString() {
return id + " " + de + " " + cai;
}
/** 获取当前学生类别 */
public int getClasses(int H) {
if (de >= H && cai >= H) return 1;
else if (de >= H && cai < H) return 2;
else if ((de < H && cai < H) && (de >= cai)) return 3;
else return 4;
}
/** 获取总分 */
public int getSum() {
return de+cai;
}
}
备注:如果你会lambda表达式,可以把匿名接口Comparator换成Lambda表达式,那样看着会更简洁。
改了一下快速输出,多过了一个测试案例。

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @题目 1015 德才论 (25分)
*
* @描述 把题目读懂。
* 第一类:H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序
* 第二类:才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;
* 第三类:德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;
* 第四类:其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。
* 第五类就不管他了。
* 这题难在最后的排序上:当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。
*
* 涉及知识点:面向对象、快速输入、集合、复杂排序。
* @author ChangSheng
* @date 2020-02-16
*/
public class Main {
public static void main(String[] args) throws IOException {
int N = Reader.nextInt(), L = Reader.nextInt(), H = Reader.nextInt();
// 存放分数高于L的学生
List<Student> students = new ArrayList<>();
for (int i = 0; i < N; i++) {
// 当前学生准考证号、德分、才分
int id = Reader.nextInt(), de = Reader.nextInt(), cai = Reader.nextInt();
if (de >= L && cai >= L) {
students.add(new Student(id, de, cai));
}
}
// 排序
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 分别比较类别,总分,德分,准考证
if (s1.getClasses(H) == s2.getClasses(H)) {
if (s1.getSum() == s2.getSum()) {
if (s1.de == s2.de) {
return s1.id - s2.id;
}
return s2.de - s1.de;
}
return s2.getSum() - s1.getSum();
}
return s1.getClasses(H) - s2.getClasses(H);
}
});
// 输出
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
out.println(students.size());
for (Student s : students) {
out.println(s);
}
out.flush();
}
/**
* 分词器快速输入
*/
static class Reader {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int nextInt() throws IOException {
in.nextToken();
return (int)in.nval;
}
static String next() throws IOException {
in.nextToken();
return in.sval;
}
}
}
/** 学生类 */
class Student {
/** 准考证、德分、才分 */
int id, de, cai;
public Student(int id, int de, int cai) {
this.id = id;
this.de = de;
this.cai = cai;
}
@Override
public String toString() {
return id + " " + de + " " + cai;
}
/** 获取当前学生类别 */
public int getClasses(int H) {
if (de >= H && cai >= H) return 1;
else if (de >= H && cai < H) return 2;
else if ((de < H && cai < H) && (de >= cai)) return 3;
else return 4;
}
/** 获取总分 */
public int getSum() {
return de+cai;
}
}
其他
参考
https://blog.csdn.net/TonyHTY/article/details/102535315
&spm=1001.2101.3001.5002&articleId=104640898&d=1&t=3&u=81ffc2c7cd614c47ab9e2b2a1fa7cc93)
2万+

被折叠的 条评论
为什么被折叠?



