洛谷题目**使用面向对象继承概念**的OOP/OOD 相关题目解答 【from 黄老师】
洛谷题目提供使用面向对象继承概念的Java解答。每个解答都将展示合理的类层次结构设计。
1. P5740 【深基7.例9】最厉害的学生 (Java OOP 解法)
import java.util.Scanner;
// 基类:学生
class Student {
protected String name;
protected int chinese;
protected int math;
protected int english;
public Student(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
// 计算总分
public int getTotal() {
return chinese + math + english;
}
// 获取信息字符串
public String getInfo() {
return name + " " + chinese + " " + math + " " + english;
}
}
// 派生类:用于查找最厉害的学生
class TopStudentFinder {
private Student topStudent;
private int topScore;
public TopStudentFinder() {
this.topScore = -1;
this.topStudent = null;
}
public void processStudent(Student stu) {
int currentScore = stu.getTotal();
if (currentScore > topScore) {
topScore = currentScore;
topStudent = stu;
}
}
public String getTopStudentInfo() {
return topStudent != null ? topStudent.getInfo() : "";
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
TopStudentFinder finder = new TopStudentFinder();
for (int i = 0; i < n; i++) {
String name = sc.next();
int chinese = sc.nextInt();
int math = sc.nextInt();
int english = sc.nextInt();
Student stu = new Student(name, chinese, math, english);
finder.processStudent(stu);
}
System.out.println(finder.getTopStudentInfo());
sc.close();
}
}
2. P5742 【深基7.例11】评等级 (Java OOP 解法)
import java.util.Scanner;
// 基类:学生
class Student {
protected int id;
protected int academic; // 学业成绩
protected int quality; // 素质拓展成绩
public Student(int id, int academic, int quality) {
this.id = id;
this.academic = academic;
this.quality = quality;
}
// 计算综合分数(避免浮点误差)
public int getCompositeScore() {
return academic * 7 + quality * 3; // 相当于 (academic*0.7 + quality*0.3) * 10
}
// 计算总分
public int getTotalScore() {
return academic + quality;
}
}
// 派生类:带评级功能的学生
class RatedStudent extends Student {
public RatedStudent(int id, int academic, int quality) {
super(id, academic, quality);
}
// 判断是否优秀
public boolean isExcellent() {
return getTotalScore() > 140 && getCompositeScore() >= 800;
}
// 获取评级结果
public String getRating() {
return isExcellent() ? "Excellent" : "Not excellent";
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int id = sc.nextInt();
int academic = sc.nextInt();
int quality = sc.nextInt();
RatedStudent student = new RatedStudent(id, academic, quality);
System.out.println(student.getRating());
}
sc.close();
}
}
3. P1093 [NOIP2007 普及组] 奖学金 (Java OOP 解法)
import java.util.*;
// 基类:学生
class Student {
protected int id;
protected int chinese;
protected int math;
protected int english;
protected int total;
public Student(int id, int chinese, int math, int english) {
this.id = id;
this.chinese = chinese;
this.math = math;
this.english = english;
this.total = chinese + math + english;
}
public int getId() { return id; }
public int getTotal() { return total; }
public int getChinese() { return chinese; }
}
// 派生类:可比较的学生(用于排序)
class ComparableStudent extends Student implements Comparable<ComparableStudent> {
public ComparableStudent(int id, int chinese, int math, int english) {
super(id, chinese, math, english);
}
@Override
public int compareTo(ComparableStudent other) {
// 先按总分从高到低排序
if (this.total != other.total) {
return other.total - this.total;
}
// 总分相同,按语文成绩从高到低排序
if (this.chinese != other.chinese) {
return other.chinese - this.chinese;
}
// 总分和语文都相同,按学号从小到大排序
return this.id - other.id;
}
@Override
public String toString() {
return id + " " + total;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<ComparableStudent> students = new ArrayList<>();
for (int i = 1; i <= n; i++) {
int chinese = sc.nextInt();
int math = sc.nextInt();
int english = sc.nextInt();
students.add(new ComparableStudent(i, chinese, math, english));
}
// 排序
Collections.sort(students);
// 输出前5名
for (int i = 0; i < Math.min(5, students.size()); i++) {
System.out.println(students.get(i));
}
sc.close();
}
}
4. P5250 【深基17.例5】木材仓库 (Java OOP 解法)
import java.util.*;
// 基类:木材
class Wood implements Comparable<Wood> {
protected int length;
public Wood(int length) {
this.length = length;
}
public int getLength() {
return length;
}
// 用于TreeSet排序
@Override
public int compareTo(Wood other) {
return Integer.compare(this.length, other.length);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Wood wood = (Wood) obj;
return length == wood.length;
}
@Override
public int hashCode() {
return Integer.hashCode(length);
}
}
// 派生类:木材仓库管理
class WoodWarehouse {
private TreeSet<Wood> warehouse;
public WoodWarehouse() {
warehouse = new TreeSet<>();
}
// 进货操作
public String addWood(int length) {
Wood wood = new Wood(length);
if (warehouse.contains(wood)) {
return "Already Exist";
}
warehouse.add(wood);
return null; // 成功添加不需要输出
}
// 出货操作
public String removeWood(int length) {
if (warehouse.isEmpty()) {
return "Empty";
}
Wood target = new Wood(length);
// 检查是否有正好长度的木材
if (warehouse.contains(target)) {
warehouse.remove(target);
return String.valueOf(length);
}
// 没有正好长度的,找最接近的
Wood floor = warehouse.floor(target); // 小于等于的最大值
Wood ceiling = warehouse.ceiling(target); // 大于等于的最小值
Wood toRemove = null;
if (floor == null) {
toRemove = ceiling;
} else if (ceiling == null) {
toRemove = floor;
} else {
int diffFloor = length - floor.getLength();
int diffCeiling = ceiling.getLength() - length;
if (diffFloor < diffCeiling) {
toRemove = floor;
} else if (diffFloor > diffCeiling) {
toRemove = ceiling;
} else {
// 距离相等,取较短的一根
toRemove = floor;
}
}
if (toRemove != null) {
int removedLength = toRemove.getLength();
warehouse.remove(toRemove);
return String.valueOf(removedLength);
}
return "Empty";
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
WoodWarehouse warehouse = new WoodWarehouse();
for (int i = 0; i < m; i++) {
int op = sc.nextInt();
int length = sc.nextInt();
if (op == 1) {
String result = warehouse.addWood(length);
if (result != null) {
System.out.println(result);
}
} else if (op == 2) {
System.out.println(warehouse.removeWood(length));
}
}
sc.close();
}
}
5. P7176 [COCI2014-2015#4] PRIPREME (Java OOP 解法)
import java.util.*;
// 基类:讲解任务
class PresentationTask {
protected int timeRequired;
public PresentationTask(int timeRequired) {
this.timeRequired = timeRequired;
}
public int getTimeRequired() {
return timeRequired;
}
}
// 派生类:时间调度计算器
class ScheduleCalculator {
private List<PresentationTask> tasks;
public ScheduleCalculator(List<PresentationTask> tasks) {
this.tasks = tasks;
}
// 计算最少需要的时间
public long calculateMinTime() {
long totalTime = 0;
int maxTime = 0;
for (PresentationTask task : tasks) {
int time = task.getTimeRequired();
totalTime += time;
if (time > maxTime) {
maxTime = time;
}
}
// 核心逻辑:如果最长的任务时间大于其他所有任务时间之和
// 那么最小时间就是 2 * maxTime
// 否则就是 totalTime
if (maxTime > totalTime - maxTime) {
return 2L * maxTime;
} else {
return totalTime;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<PresentationTask> tasks = new ArrayList<>();
for (int i = 0; i < n; i++) {
int time = sc.nextInt();
tasks.add(new PresentationTask(time));
}
ScheduleCalculator calculator = new ScheduleCalculator(tasks);
System.out.println(calculator.calculateMinTime());
sc.close();
}
}
面向对象设计说明:
- P5740:使用
Student基类和TopStudentFinder处理器类,体现了单一职责原则。 - P5742:通过
RatedStudent继承Student,添加评级功能,展示了继承的扩展性。 - P1093:
ComparableStudent继承Student并实现Comparable接口,便于排序。 - P5250:设计
Wood类和WoodWarehouse仓库管理类,使用TreeSet实现自动排序和快速查找。 - P7176:通过
PresentationTask和ScheduleCalculator分离数据与逻辑,提高代码可维护性。

浙公网安备 33010602011771号