22201134-于子昂 关于第三阶段PTA题目集的blog
目录:
(1)关于PTA题目集
(2)对本门课的意见
本次与上次blog之间时间跨度较大,故攒下了不少可以拿来说一说的题目。
这几次作业用得比较频繁的知识点有用正则表达式来判断输入信息是否合法,利用Comparator接口进行重写排序,数组主要用的是ArrayList类,尝试了HashMap类以及HashSet类的使用,明白了HashMap类适合查找信息而HashSet类适合存储信息,更加熟悉了String相关字符串类的运用,明白了Comparator接口也可用于HashMap类的排序,进一步了解了子类与父类的使用方法和关系,进一步了解了有无父类的区别,还用到了IntegerStack接口的改写。
关于题量,个人感觉还好,主要那些迭代题得在第一次写的时候就得好好写,后续的迭代才能轻松些。别的一些小题有没见过的方法上网好好查下也没啥问题了。
关于难度,大体上都能上90,想要满分得一个个试哪种情况还没考虑到,不容易的,毕竟我是遇上迭代就没满分了。其余的小题确实不难。
关于本次迭代的成绩计算系列题目:
主要给第一次的:
某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修
考核方式输入选项:考试、考察
课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出
格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
参考类图:
我的代码:
import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] args) {
Courseselection courseselection = new Courseselection();
courseselection.play();
}
}
class Class_ {
private String num;
private ArrayList<Student> students = new ArrayList<>();
public Class_(String num) {
this.num = num;
}
public String getNum() {
return num;
}
public ArrayList<Student> getStudents() {
return students;
}
public void addStudent(Student student) {
this.students.add(student);
}
public void showScore(){
boolean flag = false;
int num = 0;
int averageScore = 0;
for(int i = 0; i < this.students.size(); i ++){
if(this.students.get(i).getScores().size() != 0){
flag = true;
num = num + 1;
averageScore = averageScore + students.get(i).averageScore();
}
}
if(flag)
System.out.println(this.num + " " + averageScore / num);
else
System.out.println(this.num + " has no grades yet");
}
}
abstract class Score {
protected abstract int sumScore();
protected abstract int usualScore();
protected abstract int finalScore();
}
class Test_grades extends Score{
private int usualScore;
private int finalScore;
public Test_grades(int usualScore, int finalScore) {
this.usualScore = usualScore;
this.finalScore = finalScore;
}
@Override
public int sumScore(){
return (int)(usualScore * 0.3 + finalScore * 0.7);
}
@Override
public int usualScore(){
return usualScore;
}
@Override
public int finalScore(){
return finalScore;
}
}
class Examine_grades extends Score{
private int examScore;
public Examine_grades(int examScore) {
this.examScore = examScore;
}
@Override
public int sumScore(){
return examScore;
}
@Override
public int usualScore(){
return 0;
}
@Override
public int finalScore(){
return 0;
}
}
class Course {
private String name;
private String quality;
private String way;
private ArrayList<Score> scores = new ArrayList<>();
public Course(String name, String quality, String way) {
this.name = name;
this.quality = quality;
this.way = way;
}
public String getName() {
return name;
}
public String getWay() {
return way;
}
public void showScore(){
int averageScore = 0;
if(scores.size() == 0)
System.out.println(this.name + " has no grades yet");
else {
if (this.way.equals("考察")) {
for (int i = 0; i < scores.size(); i++)
averageScore = averageScore + scores.get(i).sumScore();
System.out.println(name + " " + averageScore / scores.size() + " " + averageScore / scores.size());
}
if (this.way.equals("考试")) {
int averageUsualScore = 0, averageFinalScore = 0;
for (int i = 0; i < scores.size(); i++) {
averageScore = averageScore + scores.get(i).sumScore();
averageUsualScore = averageUsualScore + scores.get(i).usualScore();
averageFinalScore = averageFinalScore + scores.get(i).finalScore();
}
System.out.println(name + " " + averageUsualScore / scores.size() + " " + averageFinalScore / scores.size() + " " + averageScore / scores.size());
}
}
}
public void addScore(Score score){
this.scores.add(score);
}
public boolean check(){
if(quality.equals("必修") && way.equals("考察")){
System.out.println(name + " : course type & access mode mismatch");
return false;
}
if(getStringLengthRegex(name) > 10 || !(quality.equals("必修") || quality.equals("选修")) || !(way.equals("考试") || way.equals("考察"))){
System.out.println("wrong format");
return false;
}
return true;
}
public static int getStringLengthRegex(String s) {
s = s.replaceAll("[^\\x00-\\xff]", "**");
int length = s.length();
return length;
}
}
class Student {
private String studyNum;
private String name;
private ArrayList<String> courses = new ArrayList<>();
private ArrayList<Score> scores = new ArrayList<>();
public Student(String studyNum, String name) {
this.studyNum = studyNum;
this.name = name;
}
public String getStudyNum() {
return studyNum;
}
public String getName() {
return name;
}
public ArrayList<String> getCourses() {
return courses;
}
public void addCourse(String course) {
this.courses.add(course);
}
public ArrayList<Score> getScores() {
return scores;
}
public void addScore(Score score) {
this.scores.add(score);
}
public int averageScore(){
int averageScore = 0;
for (int i = 0; i < scores.size(); i++) {
averageScore = averageScore + scores.get(i).sumScore();
}
return averageScore / scores.size();
}
public void showScore(){
if(this.scores.size() == 0)
System.out.println(this.studyNum + " " + this.name + " did not take any exams");
else
System.out.println(this.studyNum + " " + this.name + " " + this.averageScore());
}
}
class Courseselection {
private ArrayList<Class_> class_s = new ArrayList<>();
private ArrayList<Student> students = new ArrayList<>();
private ArrayList<Course> courses = new ArrayList<>();
Scanner input = new Scanner(System.in);
public Courseselection() {
}
public void play(){
String line = input.nextLine();
String[] information = line.split(" ");
Course courseNew;
while(information.length == 3 || (information.length == 2 && information[2].equals("必修"))) {
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getName().equals(information[0])) {
if(information.length == 3)
courseNew = new Course(information[0], information[1], information[2]);
else
courseNew = new Course(information[0], information[1], "必修");
courseNew.check();
line = input.nextLine();
information = line.split(" ");
i = 0;
}
if (information.length != 3 && !(information.length == 2 && information[2].equals("必修")))
break;
}
if (information.length != 3 && !(information.length == 2 && information[2].equals("必修")))
break;
if(information.length == 3)
courseNew = new Course(information[0], information[1], information[2]);
else
courseNew = new Course(information[0], information[1], "必修");
if (courseNew.check())
courses.add(courseNew);
line = input.nextLine();
information = line.split(" ");
}
while(!information[0].equals("end")) {
int i;
Score scoreNew = new Examine_grades(0);
boolean flag = false, flag1 = false, flag2 = false, flag4 = false;
for(int i2 = 0; i2 < students.size(); i2 ++){
if(students.get(i2).getStudyNum().equals(information[0]) && students.get(i2).getName().equals(information[1]))
for (int i1 = 0; i1 < students.get(i2).getCourses().size(); i1++)
if (students.get(i2).getCourses().get(i1).equals(information[2])) {
line = input.nextLine();
information = line.split(" ");
i2 = 0;
break;
}
if(information[0].equals("end"))
break;
}
if(information[0].equals("end"))
break;
if (information[0].length() != 8 || information[1].length() > 10) {
System.out.println("wrong format");
} else {
Student studentNew = new Student(information[0], information[1]);
for (i = 0; i < courses.size(); i++)
if (information[2].equals(courses.get(i).getName())) {
flag1 = true;
if (courses.get(i).getWay().equals("考试")) {
if (information.length != 5) {
System.out.println(information[0] + " " + information[1] + " : access mode mismatch");
} else {
if (Double.valueOf(information[3]) < 0 || Double.valueOf(information[4]) < 0 || Double.valueOf(information[3]) > 100 || Double.valueOf(information[4]) > 100) {
System.out.println("wrong format");
flag4 = true;
break;
}
double grade1 = Double.valueOf(information[3]);
double grade2 = Double.valueOf(information[4]);
courses.get(i).addScore(new Test_grades((int)grade1, (int)grade2));
studentNew.addCourse(information[2]);
scoreNew = new Test_grades((int)grade1, (int)grade2);
flag = true;
}
break;
} else if (courses.get(i).getWay().equals("考察")) {
if (information.length != 4) {
System.out.println(information[0] + " " + information[1] + " : access mode mismatch");
} else {
if (Double.valueOf(information[3]) < 0 || Double.valueOf(information[3]) > 100) {
System.out.println("wrong format");
flag4 = true;
break;
}
double grade = Double.valueOf(information[3]);
courses.get(i).addScore(new Examine_grades((int)grade));
studentNew.addCourse(information[2]);
scoreNew = new Examine_grades((int)grade);
flag = true;
}
break;
}
}
if (!flag4) {
for (int i1 = 0; i1 < students.size(); i1++) {
if (information[0].equals(students.get(i1).getStudyNum()) && information[1].equals(students.get(i1).getName())) {
if(flag) {
students.get(i1).addCourse(information[2]);
students.get(i1).addScore(scoreNew);
}
flag2 = true;
break;
}
}
if (!flag2) {
if (flag)
studentNew.addScore(scoreNew);
students.add(studentNew);
}
if (!flag1)
System.out.println(information[2] + " does not exist");
}
}
line = input.nextLine();
information = line.split(" ");
}
for (int i = 0; i < students.size(); i++) {
boolean flag = false;
String classnumNew = students.get(i).getStudyNum().substring(0, 6);
for (int i1 = 0; i1 < class_s.size(); i1++) {
if (class_s.get(i1).getNum().equals(classnumNew)) {
class_s.get(i1).addStudent(students.get(i));
flag = true;
break;
}
}
if (!flag) {
class_s.add(new Class_(classnumNew));
class_s.get(class_s.size() - 1).addStudent(students.get(i));
}
}
Comparator<Student> comparator1 = new Comparator<Student>(){
public int compare(Student s1, Student s2) {
return Integer.valueOf(s1.getStudyNum()) - Integer.valueOf(s2.getStudyNum());
}
};
Comparator<Object> comparator2 = Collator.getInstance(Locale.CHINA);
Collections.sort(courses, (e1, e2) -> {
return comparator2.compare(e1.getName(), e2.getName());
});
Comparator<Class_> comparator3 = new Comparator<Class_>(){
public int compare(Class_ c1, Class_ c2) {
return Integer.valueOf(c1.getNum()) - Integer.valueOf(c2.getNum());
}
};
Collections.sort(students, comparator1);
Collections.sort(class_s, comparator3);
for(int i = 0; i < students.size(); i ++
