pta题目集6-8
1.前言:这次pta较之前的点菜程序难度有所减小,但是有更多需要注意的细节,并且有很多测试点非常的极限。
题目集6:
某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重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) { ArrayList<course> courses = new ArrayList<>(); ArrayList<Student> students = new ArrayList<>(); ArrayList<Classroom> classrooms = new ArrayList<>(); Scanner input = new Scanner(System.in); choose[] chooses = new choose[20]; int p = 0, m=0; for (int i = 0; i < 20; i++) { chooses[i] = new choose(); } boolean ifNewCourse = true, ifNewStudent = true, ifNewClass = true; String[] same = new String[20]; while (true) { String st = input.nextLine(); boolean re = false; same[m] = st; for (int i = 0; i < m; i++) { if (same[i].equals(st)) { re = true; } } if(re){continue;} String[] temp = st.split(" "); int s = temp.length; if (st.equals("end")) { break; } if (s == 3) { course course1 = new course(); if (course1.getCourse(temp[0], temp[1], temp[2])) {boolean courseSame=false; if(!ifNewCourse){ for (int i=0;i<courses.size();i++) { if(temp[0].equals(courses.get(i).name)) {courseSame = true;} } } if(courseSame){continue;} courses.add(course1); ifNewCourse = false; } } if (s == 4) {boolean chooseSame= false; chooses[p].Choose = temp; for(int i=0;i<p;i++) { if((chooses[i].Choose[0] == temp[0])&&(chooses[i].Choose[1] ==temp[1])&&(chooses[i].Choose[2] == temp[2])) { chooseSame = true; } } if(chooseSame){continue;} if (chooses[p].getChoose(courses, temp, students, classrooms)) { for (int i = 0; i < courses.size(); i++) { if (courses.get(i).name.equals(temp[2])) courses.get(i).add(Integer.parseInt(temp[3])); } if (ifNewStudent) { Student student1 = new Student(); student1.getStudent(temp); students.add(student1); ifNewStudent = false; } else { boolean New = true; for (int i = 0; i < students.size(); i++) { if (students.get(i).number.equals(temp[0])) { students.get(i).add(Integer.parseInt(temp[3])); New = false; break; } } if (New) { Student student1 = new Student(); student1.getStudent(temp); students.add(student1); } } if (ifNewClass) { Classroom classroom1 = new Classroom(); classroom1.getClass(temp); classrooms.add(classroom1); ifNewClass = false; } else { boolean New = true; for (int i = 0; i < classrooms.size(); i++) { if (classrooms.get(i).number.equals(temp[0].substring(0, 6))) { classrooms.get(i).add(Integer.parseInt(temp[3])); New = false; break; } } if (New) { Classroom classroom1 = new Classroom(); classroom1.getClass(temp); classrooms.add(classroom1); } } p++; } } if (s == 5) {boolean chooseSame= false; chooses[p].Choose = temp; for(int i=0;i<p;i++) { if((chooses[i].Choose[0] == temp[0])&&(chooses[i].Choose[1] ==temp[1])&&(chooses[i].Choose[2] == temp[2])) { chooseSame = true; } } if(chooseSame){continue;} if (chooses[p].GetChoose(courses, temp, students, classrooms)) { int a = Integer.parseInt(temp[3]), b = Integer.parseInt(temp[4]); int c = (int) (a * 0.3 + b * 0.7); for (int i = 0; i < courses.size(); i++) { if (courses.get(i).name.equals(temp[2])) courses.get(i).Add(a, b); } if (ifNewStudent) { Student student1 = new Student(); student1.GetStudent(temp, c); students.add(student1); ifNewStudent = false; } else { boolean New = true; for (int i = 0; i < students.size(); i++) { if (students.get(i).number.equals(temp[0])) { students.get(i).add(c); New = false; break; } } if (New) { Student student1 = new Student(); student1.GetStudent(temp, c); students.add(student1); } } if (ifNewClass) { Classroom classroom1 = new Classroom(); classroom1.GetClass(temp, c); classrooms.add(classroom1); ifNewClass = false; } else { boolean New = true; for (int i = 0; i < classrooms.size(); i++) { if (classrooms.get(i).number.equals(temp[0].substring(0, 6))) { classrooms.get(i).add(c); New = false; break; } } if (New) { Classroom classroom1 = new Classroom(); classroom1.GetClass(temp, c); classrooms.add(classroom1); } } p++; } } m++; } Collections.sort(students, new studentComparator()); Collections.sort(classrooms, new classroomComparator()); Collections.sort(courses, new courseComparator()); for (int i = 0; i < students.size(); i++) { if (students.get(i).total != 0) { System.out.println(students.get(i).number + " " + students.get(i).name + " " + students.get(i).total / students.get(i).num); } else { System.out.println(students.get(i).number + " " + students.get(i).name + " " + "did not take any exams"); } } for (int i = 0; i < courses.size(); i++) { if (courses.get(i).usual != 0 || courses.get(i).exam != 0) { if (courses.get(i).required) { ; courses.get(i).Print(); } else { courses.get(i).print(); } } else { System.out.println(courses.get(i).name+" "+"has no grades yet"); } } for (int i = 0; i < classrooms.size(); i++) { if (classrooms.get(i).total != 0) { classrooms.get(i).print(); } else { System.out.println(classrooms.get(i).number+" "+"has no grades yet"); } } } } class studentComparator implements Comparator<Student> { public int compare(Student student1, Student student2) { return Integer.parseInt(student1.number)-Integer.parseInt(student2.number); } } class classroomComparator implements Comparator<Classroom> { public int compare(Classroom classroom1, Classroom classroom2) { return Integer.parseInt(classroom1.number)-Integer.parseInt(classroom2.number); } } class courseComparator implements Comparator<course> { @Override public int compare(course o1, course o2) { Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); return compare.compare(o1.name, o2.name);} } class Student { String number; String name; int total; int num; Student() { this.total=0; this.num=0; } public void getStudent(String[] temp) { this.number = temp[0]; this.name = temp[1]; this.total = Integer.parseInt(temp[3]); num++; } public void GetStudent(String[] temp, int a) { this.number = temp[0]; this.name = temp[1]; this.total = a; num++; } public void add(int a) { total+=a; num++; } } class Classroom { String number; int total; int num; Classroom() { num=0; total=0; } public void getClass(String[] temp) { this.number = temp[0].substring(0,6); this.total = Integer.parseInt(temp[3]); num++; } public void GetClass(String[] temp, int a) { this.number = temp[0].substring(0,6); this.total = a; num++; } public void add(int a) { total+=a; num++; } public void print() { System.out.println(number+" "+total/num); } } class course { String name; boolean required; int usual; int exam; int total; int num; public boolean getCourse(String name, String Required, String way) { if(name.length()>10){System.out.println("wrong format");return false;} this.name = name; if(Required.equals("必修")) { if(way.equals("考察")) { System.out.println(name+" : course type & access mode mismatch"); return false; } this.required = true; } if(Required.equals("选修")) { if(way.equals("考察")) { this.required = false; } else { this.required = true; } } return true; } public void add(int a) { usual+=a; num++; } public void print() { total=usual/num; System.out.println(name+" "+total+" "+total); } public void Add(int a, int b) { usual+=a; exam+=b; num++; } public void Print() { total= (int) ((int)usual/num*0.3+(int)exam/num*0.7); System.out.println(name+" "+(int)usual/num+" "+(int)exam/num+" "+total); } } class choose { Student student; Classroom classroom; course course; String[] Choose; choose() { student = new Student(); classroom = new Classroom(); course = new course(); } public boolean getChoose(ArrayList<course> courses, String[] temp, ArrayList<Student> students, ArrayList<Classroom> classrooms) { int p=0,i=0; int a = Integer.parseInt(temp[3]); if(temp[0].length()!=8||temp[1].length()>10||a<0||a>100){System.out.println("wrong format");return false;} for(;i<courses.size();i++) { if(temp[2].equals(courses.get(i).name)&&courses.get(i).required) { Student student1 = new Student();student1.number=temp[0];student1.name=temp[1];students.add(student1); Classroom classroom1 = new Classroom();classroom1.number=temp[0].substring(0,6);classrooms.add(classroom1); System.out.println(temp[0]+" "+temp[1]+" : access mode mismatch");return false;} if(!courses.get(i).name.equals(temp[2])){p++;} } if(i==0||p==i) { Student student1 = new Student();student1.number=temp[0];student1.name=temp[1];students.add(student1); Classroom classroom1 = new Classroom();classroom1.number=temp[0].substring(0,6);classrooms.add(classroom1); System.out.println(temp[2]+" does not exist");return false;} return true; } public boolean GetChoose(ArrayList<course> courses, String[] temp, ArrayList<Student> students, ArrayList<Classroom> classrooms) { int p=0,i=0; int a = Integer.parseInt(temp[3]); int b = Integer.parseInt(temp[4]); if(temp[0].length()!=8||temp[1].length()>10||a<0||a>100||b<0||b>100){System.out.println("wrong format");return false;} for(;i<courses.size();i++) { if((temp[2].equals(courses.get(i).name))&&(!courses.get(i).required)) { Student student1 = new Student();student1.number=temp[0];student1.name=temp[1];students.add(student1); Classroom classroom1 = new Classroom();classroom1.number=temp[0].substring(0,6);classrooms.add(classroom1); System.out.println(temp[0]+" "+temp[1]+temp[2]+" : access mode mismatch");return false;} if(!courses.get(i).name.equals(temp[2])){p++;} } if(i==0||p==i) { Student student1 = new Student();student1.number=temp[0];student1.name=temp[1];students.add(student1); Classroom classroom1 = new Classroom();classroom1.number=temp[0].substring(0,6);classrooms.add(classroom1); System.out.println(temp[2]+" does not exist");return false;} return true; } }
在完成这次作业时由于时间较紧张,代码未能很好地完善,最终堪堪及格,许多较为极限的测试点过不去。
题目集6:
课程成绩统计程序-2在第一次的基础上增加了实验课,以下加粗字体显示为本次新增的内容。
某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
实验的总成绩等于课程每次实验成绩的平均分
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修、实验
考核方式输入选项:考试、考察、实验
考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
考试/考查课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
实验课程成绩信息包括:学号、姓名、课程名称、实验次数、每次成绩
实验次数至少4次,不超过9次
实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+实验次数+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩
以上信息的相关约束:
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)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
import java.util.*; import java.text.*; public class Main { public static void main(String[] args) { ArrayList<course> courses = new ArrayList<>(); ArrayList<Student> students = new ArrayList<>(); ArrayList<Classroom> classrooms = new ArrayList<>(); Scanner input = new Scanner(System.in); choose[] chooses = new choose[30]; int p = 0, m=0; for (int i = 0; i < 30; i++) { chooses[i] = new choose(); } boolean ifNewCourse = true, ifNewStudent = true, ifNewClass = true; String[] same = new String[30]; while (true) { String st = input.nextLine(); boolean re = false; same[m] = st; for (int i = 0; i < m; i++) { if (same[i].equals(st)) { re = true; } } if(re){continue;} String[] temp = st.split(" "); int s = temp.length; if (st.equals("end")) { break; } if(s>5) {boolean chooseSame= false; int sum = 0; chooses[p].Choose = temp; for(int i=0;i<p;i++) { if((chooses[i].Choose[0].equals(temp[0]))&&(chooses[i].Choose[1].equals(temp[1]))&&(chooses[i].Choose[2].equals(temp[2]))) { chooseSame = true; } } if(chooseSame){continue;} boolean judge = true; if(judge) { boolean out = false; if(Integer.parseInt(temp[3])<4||Integer.parseInt(temp[3])>9){System.out.println("wrong format");continue;} if(temp.length!=(Integer.parseInt(temp[3])+4)) { Student student1 = new Student();student1.number=temp[0];student1.name=temp[1];students.add(student1); Classroom classroom1 = new Classroom();classroom1.number=temp[0].substring(0,6);classrooms.add(classroom1); System.out.println(temp[0]+" "+temp[1]+" : access mode mismatch");continue;} for(int i=4;i<temp.length;i++) { if(Integer.parseInt(temp[i])<=100&&Integer.parseInt(temp[i])>=0) { sum+=Integer.parseInt(temp[i]); } else {out=true;} } sum = (sum/Integer.parseInt(temp[3])); if(out){System.out.println("wrong format");continue;} for (int i = 0; i < courses.size(); i++) { if (courses.get(i).name.equals(temp[2])&&courses.get(i).type){courses.get(i).ADD(sum);} } if (ifNewStudent) { Student student1 = new Student(); student1.GetStudent(temp, sum); students.add(student1); ifNewStudent = false; } else { boolean New = true; for (int i = 0; i < students.size(); i++) { if (students.get(i).number.equals(temp[0])) { students.get(i).add(sum); New = false; break; } } if (New) { Student student1 = new Student(); student1.GetStudent(temp, sum); students.add(student1); } } if (ifNewClass) { Classroom classroom1 = new Classroom(); classroom1.GetClass(temp, sum); classrooms.add(classroom1); ifNewClass = false; } else { boolean New = true; for (int i = 0; i < classrooms.size(); i++) { if (classrooms.get(i).number.equals(temp[0].substring(0, 6))) { classrooms.get(i).add(sum); New = false; break; } } if (New) { Classroom classroom1 = new Classroom(); classroom1.GetClass(temp, sum); classrooms.add(classroom1); } } p++; } } if (s == 3) { course course1 = new course(); if (course1.getCourse(temp[0], temp[1], temp[2])) {boolean courseSame=false; if(!ifNewCourse){ for (int i=0;i<courses.size();i++) { if(temp[0].equals(courses.get(i).name)) {courseSame = true;} } } if(courseSame){continue;} courses.add(course1); ifNewCourse = false; } } if (s == 4) {boolean chooseSame= false; chooses[p].Choose = temp; for(int i=0;i<p;i++) { if((chooses[i].Choose[0].equals(temp[0]))&&(chooses[i].Choose[1].equals(temp[1]))&&(chooses[i].Choose[2].equals(temp[2]))) { chooseSame = true; } } if(chooseSame){continue;} if (chooses[p].getChoose(courses, temp, students, classrooms)) { for (int i = 0; i < courses.size(); i++) { if (courses.get(i).name.equals(temp[2])) courses.get(i).add(Integer.parseInt(temp[3])); } if (ifNewStudent) { Student student1 = new Student(); student1.getStudent(temp); students.add(student1); ifNewStudent = false; } else { boolean New = true; for (int i = 0; i < students.size(); i++) { if (students.get(i).number.equals(temp[0])) { students.get(i).add(Integer.parseInt(temp[3])); New = false; break; } } if (New) { Student student1 = new Student(); student1.getStudent(temp); students.add(student1); } } if (ifNewClass) { Classroom classroom1 = new Classroom(); classroom1.getClass(temp); classrooms.add(classroom1); ifNewClass = false; } else { boolean New = true; for (int i = 0; i < classrooms.size(); i++) { if (classrooms.get(i).number.equals(temp[0].substring(0, 6))) { classrooms.get(i).add(Integer.parseInt(temp[3])); New = false; break; } } if (New) { Classroom classroom1 = new Classroom(); classroom1.getClass(temp); classrooms.add(classroom1); } } p++; } } if (s == 5) {boolean chooseSame= false; chooses[p].Choose = temp; for(int i=0;i<p;i++) { if((chooses[i].Choose[0].equals(temp[0]))&&(chooses[i].Choose[1].equals(temp[1]))&&(chooses[i].Choose[2].equals(temp[2]))) { chooseSame = true; } } if(chooseSame){continue;} if (chooses[p].GetChoose(courses, temp, students, classrooms)) { int a = Integer.parseInt(temp[3]), b = Integer.parseInt(temp[4]); int c = (int) (a * 0.3 + b * 0.7); for (int i = 0; i < courses.size(); i++) { if (courses.get(i).name.equals(temp[2])) courses.get(i).Add(a, b); } if (ifNewStudent) { Student student1 = new Student(); student1.GetStudent(temp, c); students.add(student1); ifNewStudent = false; } else { boolean New = true; for (int i = 0; i < students.size(); i++) { if (students.get(i).number.equals(temp[0])) { students.get(i).add(c); New = false; break; } } if (New) { Student student1 = new Student(); student1.GetStudent(temp, c); students.add(student1); } } if (ifNewClass) { Classroom classroom1 = new Classroom(); classroom1.GetClass(temp, c); classrooms.add(classroom1); ifNewClass = false; } else { boolean New = true; for (int i = 0; i < classrooms.size(); i++) { if (classrooms.get(i).number.equals(temp[0].substring(0, 6))) { classrooms.get(i).add(c); New = false; break; } } if (New) { Classroom classroom1 = new Classroom(); classroom1.GetClass(temp, c); classrooms.add(classroom1); } } p++; } } m++; } Collections.sort(students, new studentComparator()); Collections.sort(classrooms, new classroomComparator()); Collections.sort(courses, new courseComparator()); for (int i = 0; i < students.size(); i++) { if (students.get(i).total != 0) { System.out.println(students.get(i).number + " " + students.get(i).name + " " + students.get(i).total / students.get(i).num); } else { System.out.println(students.get(i).number + " " + students.get(i).name + " " + "did not take any exams"); } } for (int i = 0; i < courses.size(); i++) { if (courses.get(i).usual != 0 || courses.get(i).exam != 0 ||courses.get(i).total!=0) { if (courses.get(i).required) { courses.get(i).Print(); } if(!courses.get(i).required&&!courses.get(i).type) { courses.get(i).print(); } if(courses.get(i).type) {courses.get(i).PRINT();} } else { System.out.println(courses.get(i).name+" "+"has no grades yet"); } } for (int i = 0; i < classrooms.size(); i++) { if (classrooms.get(i).total != 0) { classrooms.get(i).print(); } else { System.out.println(classrooms.get(i).number+" "+"has no grades yet"); } } } } class studentComparator implements Comparator<Student> { public int compare(Student student1, Student student2) { return Integer.parseInt(student1.number)-Integer.parseInt(student2.number); } } class classroomComparator implements Comparator<Classroom> { public int compare(Classroom classroom1, Classroom classroom2) { return Integer.parseInt(classroom1.number)-Integer.parseInt(classroom2.number); } } class courseComparator implements Comparator<course> { @Override public int compare(course o1, course o2) { Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); return compare.compare(o1.name, o2.name);} } class Student { String number; String name; int total; int num; Student() { this.total=0; this.num=0; } public void getStudent(String[] temp) { this.number = temp[0]; this.name = temp[1]; this.total = Integer.parseInt(temp[3]); num++; } public void GetStudent(String[] temp, int a) { this.number = temp[0]; this.name = temp[1]; this.total = a; num++; } public void add(int a) { total+=a; num++; } } class Classroom { String number; int total; int num; Classroom() { num=0; total=0; } public void getClass(String[] temp) { this.number = temp[0].substring(0,6); this.total = Integer.parseInt(temp[3]); num++; } public void GetClass(String[] temp, int a) { this.number = temp[0].substring(0,6); this.total = a; num++; } public void add(int a) { total+=a; num++; } public void print() { System.out.println(number+" "+total/num); } } class course { String name; boolean required; boolean type; int usual; int exam; int total; int num; public boolean getCourse(String name, String Required, String way) { if(name.length()>10){System.out.println("wrong format");return false;} this.name = name; if(Required.equals("必修")) { if(way.equals("考察")||way.equals("实验")) { System.out.println(name+" : course type & access mode mismatch"); return false; } this.required = true; type=false; } if(Required.equals("选修")) { if(way.equals("考察")) { this.required = false; type=false; } if(way.equals("考试")) { this.required = true; type=false; } if(way.equals("实验")) { System.out.println(name+" : course type & access mode mismatch"); return false; } } if(Required.equals("实验")) { if(!way.equals("实验")) { System.out.println(name+" : course type & access mode mismatch"); return false; } this.required = false; this.type = true; } return true; } public void add(int a) { usual+=a; num++; } public void print() { total=usual/num; System.out.println(name+" "+total+" "+total); } public void Add(int a, int b) { usual+=a; exam+=b; num++; } public void Print() { total= (int) ((int)(usual/num*0.3)+(int)(exam/num*0.7)); System.out.println(name+" "+(int)usual/num+" "+(int)exam/num+" "+total); } public void ADD(int a) { total+=a; num++; } public void PRINT() { total = (int)(total/num); System.out.println(name+" "+total); } } class choose { Student student; Classroom classroom; course course; String[] Choose; choose() { student = new Student(); classroom = new Classroom(); course = new course(); } public boolean getChoose(ArrayList<course> courses, String[] temp, ArrayList<Student> students, ArrayList<Classroom> classrooms) { int p=0,i=0; int a = Integer.parseInt(temp[3]); if(temp[0].length()!=8||temp[1].length()>10||a<0||a>100){System.out.println("wrong format");return false;} for(;i<courses.size();i++) { if(temp[2].equals(courses.get(i).name)&&courses.get(i).required) { Student student1 = new Student();student1.number=temp[0];student1.name=temp[1];students.add(student1); Classroom classroom1 = new Classroom();classroom1.number=temp[0].substring(0,6);classrooms.add(classroom1); System.out.println(temp[0]+" "+temp[1]+" : access mode mismatch");return false;} if(!courses.get(i).name.equals(temp[2])){p++;} } if(i==0||p==i) { Student student1 = new Student();student1.number=temp[0];student1.name=temp[1];students.add(student1); Classroom classroom1 = new Classroom();classroom1.number=temp[0].substring(0,6);classrooms.add(classroom1); System.out.println(temp[2]+" does not exist");return false;} return true; } public boolean GetChoose(ArrayList<course> courses, String[] temp, ArrayList<Student> students, ArrayList<Classroom> classrooms) { int p=0,i=0; int a = Integer.parseInt(temp[3]); int b = Integer.parseInt(temp[4]); if(temp[0].length()!=8||temp[1].length()>10||a<0||a>100||b<0||b>100){System.out.println("wrong format");return false;} for(;i<courses.size();i++) { if((temp[2].equals(courses.get(i).name))&&(!courses.get(i).required)) { Student student1 = new Student();student1.number=temp[0];student1.name=temp[1];students.add(student1); Classroom classroom1 = new Classroom();classroom1.number=temp[0].substring(0,6);classrooms.add(classroom1); System.out.println(temp[0]+" "+temp[1]+temp[2]+" : access mode mismatch");return false;} if(!courses.get(i).name.equals(temp[2])){p++;} } if(i==0||p==i) { Student student1 = new Student();student1.number=temp[0];student1.name=temp[1];students.add(student1); Classroom classroom1 = new Classroom();classroom1.number=temp[0].substring(0,6);classrooms.add(classroom1); System.out.println(temp[2]+" does not exist");return false;} return true; } }
这次测试点较少,所以改进之后只有三个极限测试点没有通过。
题目集8:
- 输入n,然后连续输入n个身份证号。
- 然后根据输入的是sort1还是sort2,执行不同的功能。输入的不是sort1或sort2,则输出
exit
并退出。
输入sort1,将每个身份证的年月日抽取出来,按年-月-日格式组装,然后对组装后的年-月-日升序输出。
输入sort2,将所有身份证按照里面的年月日升序输出。
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner (System.in); int n; n=sc.nextInt(); String s[]=new String[n]; String birth[]=new String[n]; String str; for(int i=0;i<n;i++) { s[i]=sc.next(); } while(true) { str=sc.next(); if(str.equals("e")) { System.out.println("exit"); break; }else if(str.equals("sort1")) { for(int i=0;i<n;i++) { birth[i]=s[i].substring(6,10 )+'-'+ s[i].substring(10, 12)+'-'+s[i].substring(12, 14); } Arrays.sort(birth); for(int i=0;i<n;i++) { System.out.println(birth[i]); } }else if(str.equals("sort2")) { for(int i=0;i<n;i++) { birth[i]=s[i].substring(6,10 )+ s[i].substring(10, 12)+s[i].substring(12, 14); } Arrays.sort(birth); for(int i=0;i<n;i++) { for(int j=0;j<n;j++){ if(s[j].contains(birth[i])) { System.out.println(s[j]); break; } } } } } } }
定义IntegerStack
接口,用于声明一个存放Integer元素的栈的常见方法:
public Integer push(Integer item);
//如果item为null,则不入栈直接返回null。如果栈满,也返回null。如果插入成功,返回item。
public Integer pop(); //出栈,如果为空,则返回null。出栈时只移动栈顶指针,相应位置不置为null
public Integer peek(); //获得栈顶元素,如果为空,则返回null.
public boolean empty(); //如果为空返回true
public int size(); //返回栈中元素个数
定义IntegerStack的实现类ArrayIntegerStack
,内部使用数组实现。创建时,可指定内部数组大小。
main方法说明
- 输入n,建立可包含n个元素的ArrayIntegerStack对象
- 输入m个值,均入栈。每次入栈均打印入栈返回结果。
- 输出栈顶元素,输出是否为空,输出size
- 使用Arrays.toString()输出内部数组中的值。
- 输入x,然后出栈x次,每次出栈均打印。
- 输出栈顶元素,输出是否为空,输出size
- 使用Arrays.toString()输出内部数组中的值。
代码如下:
import java.util.*; interface IntegerStack{ public Integer push(Integer item); public Integer pop(); public Integer peek(); public boolean empty(); public int size(); } class ArrayIntegerStack implements IntegerStack{ Integer A[]; int Max,Size = 0; public ArrayIntegerStack(int n) { A = new Integer[n]; Max = n; } public Integer push(Integer item) { if(item == null) return null; else if(Size == Max) return null; else { A[Size] = item; Size++; return item; } } public Integer pop() { if(Size == 0) return null; Size--; return A[Size]; } public Integer peek() { if(Size == 0) return null; else return A[Size - 1]; } public boolean empty() { if(Size == 0) return true; else return false; } public int size() { return Size; } public String toString() { return Arrays.toString(A); } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); ArrayIntegerStack A = new ArrayIntegerStack(n); int m = in.nextInt(); for(int i = 0;i < m;i++) { System.out.println(A.push(in.nextInt())); } System.out.println(A.peek() + "," + A.empty() + "," + A.size()); System.out.println(A.toString()); int x = in.nextInt(); for(int i = 0;i < x;i++) { System.out.println(A.pop()); } System.out.println(A.peek() + "," + A.empty() + "," + A.size()); System.out.println(A.toString()); in.close(); } }
3.总结:
这几次pta的作业给了我很大的触动,首先就是可持续性改进,在课程成绩统计程序中写下的代码,在接下来的课程成绩统计程序2课程成绩统计程序3中都只要做小小的改进甚至不需要改进就能拿来用,之后只需要随着题目的改动来写出新的类和改动主类就行,这几个题目不仅增强了我对类的属性,方法的思考,而且深化了我对面对对象程序设计的理解,同时,对于idea的使用,比如调试,插件的安装与查找等技能,也得到了很好的锻炼!我的独立思考能力也逐渐增强,对写代码的热情也越来越高!我认为还需改进的地方就是代码的健壮性不足,仍需要多多钻研。我认为实验题目也同样可以让学生写一篇博客来回顾自己的学习经历,对于pta题目的话我认为可以在每次作业之后开一个补题集给学生不断的进行改进。以上是我的全部内容。