23201814 第一轮PTA作业

yukinos / 2024-04-21 / 原文

前言:

在这个学期第一次学习Java,了解到这是一个适用范围极大的语言,虽然相比于c/c++效率较低,但是更加的方便、简单。经过这三次的PTA作业,使我受益匪浅,这三次作业相比于之前的作业,代码量和难度有巨大提升。

每次的PTA作业都有4到5道题,除了最后一道题,其他的题都相对简单,可以帮助我们学习和复习知识点和语法。在第一次作业学习的类与对象的使用,在第二次作业学习了接口和抽象类,在第三次作业学习了日期类的使用。每次PTA的最后一道题都是一个巨大的挑战,它包含了各种知识点,需要一边学习,一边设计,一边实现。

PTA第一次作业的最后一题

7-5 答题判题程序-1 分数 74 困难 作者 蔡轲 单位 南昌航空大学 设计实现答题程序,模拟一个小型的测试,要求输入题目信息和答题信息,根据输入题目信息中的标准答案判断答题的结果。

输入格式:
程序输入信息分三部分:

1、题目数量

格式:整数数值,若超过1位最高位不能为0,

样例:34

2、题目内容

一行为一道题,可以输入多行数据。

格式:"#N:"+题号+" "+"#Q:"+题目内容+" "#A:"+标准答案

格式约束:题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。

样例:#N:1 #Q:1+1= #A:2

     #N:2 #Q:2+2= #A:4

3、答题信息

答题信息按行输入,每一行为一组答案,每组答案包含第2部分所有题目的解题答案,答案的顺序号与题目题号相对应。

格式:"#A:"+答案内容

格式约束:答案数量与第2部分题目的数量相同,答案之间以英文空格分隔。

样例:#A:2 #A:78

  2是题号为1的题目的答案
  78是题号为2的题目的答案

答题信息以一行"end"标记结束,"end"之后的信息忽略。

输出格式:
1、题目数量

格式:整数数值,若超过1位最高位不能为0,

样例:34

2、答题信息

一行为一道题的答题信息,根据题目的数量输出多行数据。

格式:题目内容+" ~"+答案

样例:1+1=~2

      2+2= ~4

3、判题信息

判题信息为一行数据,一条答题记录每个答案的判断结果,答案的先后顺序与题目题号相对应。

格式:判题结果+" "+判题结果

格式约束:

 1、判题结果输出只能是true或者false,
 2、判题信息的顺序与输入答题信息中的顺序相同

样例:true false true

输入样例1:
单个题目。例如:

1

#N:1 #Q:1+1= #A:2

#A:2 end 输出样例1: 在这里给出相应的输出。例如:

1+1=~2
true
输入样例2:
单个题目。例如:

1

#N:1 #Q:1+1= #A:2

#A:4 end 输出样例2: 在这里给出相应的输出。例如:

1+1=~4
false
输入样例3:
多个题目。例如:

2

#N:1 #Q:1+1= #A:2

#N:2 #Q:2+2= #A:4

#A:2 #A:4 end 输出样例3: 在这里给出相应的输出。例如:

1+1=~2
2+2=~4
true true
输入样例4:
多个题目。例如:

2

#N:1 #Q:1+1= #A:2

#N:2 #Q:2+2= #A:4

#A:2 #A:2 end 输出样例4: 在这里给出相应的输出。例如:

1+1=~2
2+2=~2
true false
输入样例5:
多个题目,题号顺序与输入顺序不同。例如:

2

#N:2 #Q:1+1= #A:2

#N:1 #Q:5+5= #A:10

#A:10 #A:2 end 输出样例5: 在这里给出相应的输出。例如:

5+5=~10
1+1=~2
true true
输入样例6:
含多余的空格符。例如:

1

#N:1 #Q: The starting point of the Long March is #A:ruijin

#A:ruijin end 输出样例6: 在这里给出相应的输出。例如:

The starting point of the Long March is~ruijin
true

输入样例7:
含多余的空格符。例如:

1

#N: 1 #Q: 5 +5= #A:10

#A:10 end 输出样例7: 在这里给出相应的输出。例如:

5 +5=~10
true

设计建议:
以下是针对以上题目要求的设计建议,其中的属性、方法为最小集,实现代码中可根据情况添加所需的内容:

题目类(用于封装单个题目的信息):

属性:题目编号、题目内容、标准答案-standardAnswer
方法:数据读写set\get方法、
判题方法(答案-answer):判断答案-answer是否符合标准答案-standardAnswer
试卷类(用于封装整套题目的信息)

属性:题目列表(题目类的对象集合)、题目数量
方法:判题方法(题号-num、答案-answer):判断答案-answer是否符合对应题号的题目标准答案-standardAnswer
保存题目(题号-num、题目-question):将题目保存到题目列表中,保存位置与num要能对应
答卷类(用于封装答题信息)

属性:试卷(试卷类的对象)、答案列表(保存每一题的答案)、判题列表(保存每一题的判题结果true/false)
方法:判题方法(题号-num):判断答案列表中第num题的结果是否符合试卷中对应题号的题目标准答案
输出方法(题号-num):按照题目的格式要求,输出题号为num的题目的内容和答题结果。
保存一个答案(题号-num,答案-answer):保存题号为num的题目的答题结果answer。
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB

这是第一次PTA作业的最后一题,相比较后两次来说是最简单的,类的设计简单明确,也是我唯一一次满分的。

源代码

点击查看代码
import java.util.*;
import java.util.Scanner;
import java.util.Map;

class Question {
    private int num;
    private String content;
    private String standardAnswer;
    public Question(int num, String content, String standardAnswer) {
        this.num = num;
        this.content = content;
        this.standardAnswer = standardAnswer;
    }
    public int getNum() {
        return num;
    }
    public String getContent() {
        return content;
    }
     public String getStandardAnswer() {
        return standardAnswer;
    }
    public boolean judge(String answer) {
       // return this.standardAnswer.trim().equalsIgnoreCase(answer.trim());
           return this.standardAnswer.equals(answer);
    }
}
class ExamPaper {
    private Map<Integer, Question> questions = new TreeMap<>();
    public void addQuestion(Question question) {
        questions.put(question.getNum(), question);
    }
    public Collection<Question> getQuestions() {
        return questions.values();
    }

    public Question getQuestion(int num) {
        return questions.get(num);
    }
}

class ExamAnswer {
    private ExamPaper examPaper;
    private Map<Integer, String> answers = new LinkedHashMap<>();
    public ExamAnswer(ExamPaper examPaper) {
        this.examPaper = examPaper;
    }
    public void addAnswer(int questionNumber, String answer) {
        answers.put(questionNumber, answer);
    }
    public void printlast() { 
     //   Question question;
        int flag=0;
        int ab=0;
        StringBuilder sb = new StringBuilder();  
        boolean isFirst = true;  
        //String answer = answers.get(question.getNum());
        Map<Integer, Boolean> judges = new HashMap<>(); 
        for ( Question question : examPaper.getQuestions()) {
           
            flag++;
            String answer = answers.get(question.getNum());
            System.out.println(question.getContent() + "~" + answer);
                    judges.put(question.getNum(), question.judge(answer));
      
        }
        for (Map.Entry<Integer, Boolean> entry :judges.entrySet()) {  
            
            if(ab==0){
                System.out.print( entry.getValue());
            }
             
            else{
             System.out.print( " "+entry.getValue());
            }
           ab++;

}
    
        }}

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i;
        int questionsum = Integer.parseInt(scanner.nextLine());
       // Integer questionsum=scanner.next();
        ExamPaper examPaper = new ExamPaper();

        for (i=0;i<questionsum; i++) {
            String[] question1 = scanner.nextLine().trim().split(" #");
            int num = Integer.parseInt(question1[0].split(":")[1].trim());
            String content = question1[1].split(":")[1].trim();
            String standardAnswer = question1[2].split(":")[1].trim();
            Question question = new Question(num,content,standardAnswer);
            examPaper.addQuestion(question);
        }
        ExamAnswer examAnswer = new ExamAnswer(examPaper);
        String[] answers = scanner.nextLine().trim().substring(3).split(" #A:");
        for (i = 0; i < answers.length; i++) {
            examAnswer.addAnswer(i+1, answers[i].trim());
        }
        examAnswer.printlast();
    }
}

类图

image

改进建议

因为这道题相对简单,因此功能实现比较完整,但还有一定的缺陷,就是没有完全实现单一变量职责,没有将字符串解析单独制作成一个类,而是直接放在主类中,后续可以改进。

PTA第二次作业最后一题

7-2 答题判题程序-2 分数 54 作者 蔡轲 单位 南昌航空大学 设计实现答题程序,模拟一个小型的测试,以下粗体字显示的是在答题判题程序-1基础上增补或者修改的内容。

要求输入题目信息、试卷信息和答题信息,根据输入题目信息中的标准答案判断答题的结果。

输入格式:

程序输入信息分三种,三种信息可能会打乱顺序混合输入:

1、题目信息

一行为一道题,可输入多行数据(多道题)。

格式:"#N:"+题目编号+" "+"#Q:"+题目内容+" "#A:"+标准答案

格式约束:

1、题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。
2、允许题目编号有缺失,例如:所有输入的题号为1、2、5,缺少其中的3号题。此种情况视为正常。

样例:#N:1 #Q:1+1= #A:2

     #N:2 #Q:2+2= #A:4

2、试卷信息

一行为一张试卷,可输入多行数据(多张卷)。

格式:"#T:"+试卷号+" "+题目编号+"-"+题目分值

 题目编号应与题目信息中的编号对应。

 一行信息中可有多项题目编号与分值。

样例:#T:1 3-5 4-8 5-2

3、答卷信息

答卷信息按行输入,每一行为一张答卷的答案,每组答案包含某个试卷信息中的题目的解题答案,答案的顺序与试卷信息中的题目顺序相对应。

格式:"#S:"+试卷号+" "+"#A:"+答案内容

格式约束:答案数量可以不等于试卷信息中题目的数量,没有答案的题目计0分,多余的答案直接忽略,答案之间以英文空格分隔。

样例:#S:1 #A:5 #A:22

   1是试卷号 

   5是1号试卷的顺序第1题的题目答案

   22是1号试卷的顺序第2题的题目答案

答题信息以一行"end"标记结束,"end"之后的信息忽略。

输出格式:

1、试卷总分警示

该部分仅当一张试卷的总分分值不等于100分时作提示之用,试卷依然属于正常试卷,可用于后面的答题。如果总分等于100分,该部分忽略,不输出。

格式:"alert: full score of test paper"+试卷号+" is not 100 points"

样例:alert: full score of test paper2 is not 100 points

2、答卷信息

一行为一道题的答题信息,根据试卷的题目的数量输出多行数据。

格式:题目内容+""+答案++""+判题结果(true/false)

约束:如果输入的答案信息少于试卷的题目数量,答案的题目要输"answer is null"

样例:3+2=5true

     4+6=~22~false.

  answer is null

3、判分信息

判分信息为一行数据,是一条答题记录所对应试卷的每道小题的计分以及总分,计分输出的先后顺序与题目题号相对应。

格式:题目得分+" "+....+题目得分+"~"+总分

格式约束:

1、没有输入答案的题目计0分

2、判题信息的顺序与输入答题信息中的顺序相同
样例:5 8 0~13

根据输入的答卷的数量以上2、3项答卷信息与判分信息将重复输出。

4、提示错误的试卷号

如果答案信息中试卷的编号找不到,则输出”the test paper number does not exist”,参见样例9。

设计建议:

参考答题判题程序-1,建议增加答题类,类的内容以及类之间的关联自行设计。

输入样例1:
一张试卷一张答卷。试卷满分不等于100。例如:

#N:1 #Q:1+1= #A:2

#N:2 #Q:2+2= #A:4

#T:1 1-5 2-8

#S:1 #A:5 #A:22 end 输出样例1: 在这里给出相应的输出。例如:

alert: full score of test paper1 is not 100 points
1+1=5false
2+2=22false
0 0~0
输入样例2:
一张试卷一张答卷。试卷满分不等于100。例如:

#N:1 #Q:1+1= #A:2

#N:2 #Q:2+2= #A:4

#T:1 1-70 2-30

#S:1 #A:5 #A:22 end 输出样例2: 在这里给出相应的输出。例如:

1+1=5false
2+2=22false
0 0~0
输入样例3:
一张试卷、一张答卷。各类信息混合输入。例如:

#N:1 #Q:1+1= #A:2

#N:2 #Q:2+2= #A:4

#T:1 1-70 2-30

#N:3 #Q:3+2= #A:5

#S:1 #A:5 #A:4 end 输出样例: 在这里给出相应的输出。例如:

1+1=5false
2+2=4true
0 30~30
输入样例4:
试卷题目的顺序与题号不一致。例如:

#N:1 #Q:1+1= #A:2

#N:2 #Q:2+2= #A:4

#T:1 2-70 1-30

#N:3 #Q:3+2= #A:5

#S:1 #A:5 #A:22 end 输出样例: 在这里给出相应的输出。例如:

2+2=5false
1+1=22false
0 0~0
输入样例5:
乱序输入。例如:

#N:3 #Q:3+2= #A:5

#N:2 #Q:2+2= #A:4

#T:1 3-70 2-30

#S:1 #A:5 #A:22

#N:1 #Q:1+1= #A:2 end 输出样例: 在这里给出相应的输出。例如:

3+2=5true
2+2=22false
70 0~70
输入样例6:
乱序输入+两份答卷。例如:

#N:3 #Q:3+2= #A:5

#N:2 #Q:2+2= #A:4

#T:1 3-70 2-30

#S:1 #A:5 #A:22

#N:1 #Q:1+1= #A:2

#S:1 #A:5 #A:4 end 输出样例: 在这里给出相应的输出。例如:

3+2=5true
2+2=22false
70 0~70
3+2=5true
2+2=4true
70 30~100
输入样例7:
乱序输入+分值不足100+两份答卷。例如:

#N:3 #Q:3+2= #A:5

#N:2 #Q:2+2= #A:4

#T:1 3-7 2-6

#S:1 #A:5 #A:22

#N:1 #Q:1+1= #A:2

#S:1 #A:5 #A:4 end 输出样例: 在这里给出相应的输出。例如:

alert: full score of test paper1 is not 100 points
3+2=5true
2+2=22false
7 0~7
3+2=5true
2+2=4true
7 6~13
输入样例8:
乱序输入+分值不足100+两份答卷+答卷缺失部分答案。例如:

#N:3 #Q:3+2= #A:5

#N:2 #Q:2+2= #A:4

#T:1 3-7 2-6

#S:1 #A:5 #A:22

#N:1 #Q:1+1= #A:2

#T:2 2-5 1-3 3-2

#S:2 #A:5 #A:4 end 输出样例: 在这里给出相应的输出。例如:

alert: full score of test paper1 is not 100 points
alert: full score of test paper2 is not 100 points
3+2=5true
2+2=22false
7 0~7
2+2=5false
1+1=4false
answer is null
0 0 0~0
输入样例9:
乱序输入+分值不足100+两份答卷+无效的试卷号。例如:

#N:3 #Q:3+2= #A:5

#N:2 #Q:2+2= #A:4

#T:1 3-7 2-6

#S:3 #A:5 #A:4 end 输出样例: 在这里给出相应的输出。例如:

alert: full score of test paper1 is not 100 points
The test paper number does not exist
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制

源代码

点击查看代码
import java.util.*;
import java.util.Scanner;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Question {
    private int num;
    private String content;
    private String standardAnswer;
    public Question(int num, String content, String standardAnswer) {
        this.num = num;
        this.content = content;
        this. standardAnswer = standardAnswer;
    }
    public int getNum() {
        return num;
    }
    public String getContent() {
        return content;
    }
     public String getStandardAnswer() {
        return standardAnswer;
    }
    public boolean judge(String answer) {
       // return this.standardAnswer.trim().equalsIgnoreCase(answer.trim());
           return this.standardAnswer.equals(answer);
    }
}
class ExamPaper {
    private int num;
    private Map<Integer, Integer> questionscore = new LinkedHashMap<>();
 //   private static Map<Integer,Question> questions=new HashMap<>();
    public void addQuestion(int questionid ,int score) {
        questionscore.put(questionid, score);
    }
    public ExamPaper(int num)
    {
        this.num=num;
    }
    
    public Map<Integer,Integer> getQuestions() {
        return questionscore;
    }

    public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getTotalScore() {  
	    int total = 0;  
	    for (int score : questionscore.values()) {  
	        total += score;  
	    }  
	    return total;  
	}
}

class ExamAnswer {
    private ExamPaper examPaper;
    private static  Map<Integer, ExamPaper> testPapers = new HashMap<>();
    private List<String> answers = new ArrayList<>();
    private int paperid;
  //  private static  List<ExamAnswer> examAnswer = new ArrayList<>();
    public ExamAnswer(int paperid,List<String> answers) {
    	this.paperid=paperid;
    	this.answers=answers;
    }
    public int getPaperId() {
    	return paperid;
    }
    public List<String> getAnswers(){
    	return answers;
    }
    public ExamAnswer(ExamPaper examPaper) {
        this.examPaper = examPaper;
    }
//    public void addAnswer(int questionNumber, String answer) {
//        answers.put(questionNumber, answer);
//    }
//    public void printlast() { 
//     //  Question question;
//        int flag=0;
//        int ab=0;
//        StringBuilder sb = new StringBuilder();  
//        boolean isFirst = true;  
//        //String answer = answers.get(question.getNum());
//        Map<Integer, Boolean> judges = new HashMap<>(); 
//        for ( Question question : examPaper.getQuestions()) {
//           
//            flag++;
//            String answer = answers.get(question.getNum());
//            System.out.println(question.getContent() + "~" + answer);
//                    judges.put(question.getNum(), question.judge(answer));
//      
//        }
//        for (Map.Entry<Integer, Boolean> entry :judges.entrySet()) {  
//            
//            if(ab==0){
//                System.out.print( entry.getValue());
//            }
//             
//            else{
//             System.out.print( " "+entry.getValue());
//            }
//           ab++;
//
//}
}
public class Main {
    private static  Map<Integer, Question> questions = new HashMap<>();
    private static Map<Integer, ExamPaper> examPapers = new HashMap<>();
    private static List<ExamAnswer> ExamAnswers = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Pattern pattern = Pattern.compile("#N:(\\d+) #Q:(.+) #A:(.+)");

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if ("end".equals(line)) {
                break;
            }

            if (line.startsWith("#N:")) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    int id = Integer.parseInt(matcher.group(1));
                    String content = matcher.group(2).trim();
                    String answer = matcher.group(3).trim();
                    Question ques=new Question(id,content,answer);
        			questions.put(id,ques );
                }
            } else if (line.startsWith("#T:")) {
                String[] parts = line.split(" ");
                int paperId = Integer.parseInt(parts[0].substring(3));
                ExamPaper examPaper = new ExamPaper(paperId);
                for (int i = 1; i < parts.length; i++) {
                    String[] questionInfo = parts[i].split("-");
                    int questionId = Integer.parseInt(questionInfo[0]);
                    int score = Integer.parseInt(questionInfo[1]);
                    examPaper.addQuestion(questionId, score);
                }
                examPapers.put(paperId, examPaper);
            } else if (line.startsWith("#S:")) {
                String[] parts = line.split(" ");
                int paperId = Integer.parseInt(parts[0].substring(3));
                List<String> answers = new ArrayList<>();
                for (int i = 1; i < parts.length; i++) {
                    String answer = parts[i].substring(3).trim();
                    answers.add(answer);
                }
                ExamAnswers.add(new ExamAnswer(paperId, answers));
            }
        }
        for (ExamAnswer examAnswer : ExamAnswers) {  
            ExamPaper examPaper = examPapers.get(examAnswer.getPaperId());  
            if (examPaper == null || examPaper.getTotalScore() != 100) {  
                System.out.println("alert: full score of test paper" + examAnswer.getPaperId() + " is not 100 points");  
            }  
        }
     
        for(ExamAnswer examAnswer: ExamAnswers) {
        	ExamPaper examPaper= examPapers.get(examAnswer.getPaperId()); 
        
        	int total=0;
        	int i=0;
        	 List<String> userAnswers = examAnswer.getAnswers();
        	 for (Map.Entry<Integer, Integer> entry : examPaper.getQuestions().entrySet()) {
                 Question question = questions.get(entry.getKey());
                 if (question != null) {
                	 String userAnswer;  
                	 int flag1=0;
                	 if (i < userAnswers.size()) {  
                	     userAnswer = userAnswers.get(i);  
                	 } else {   
                	     userAnswer = "answer is null";  
                	     flag1=1;
                	 }
                     boolean flag = question.judge(userAnswer);
                     if (flag) {
                         total += entry.getValue();
                         System.out.println(question.getContent() + "~" + userAnswer + "~true");
                     } else {
                    	 if(flag1==0) {
                    		 System.out.println(question.getContent() + "~" + userAnswer + "~false");
                    	 }
                    	 if(flag1==1) {
                    		 System.out.println( userAnswer );
                    	 }
                         
                     }
                 } else {
                     System.out.println("answer is null");
                 }
                 i++;
             } 
        	 
             StringBuilder prints = new StringBuilder();
             int last = 0;
             for (Integer score : examPaper.getQuestions().values()) {
                 if (last < userAnswers.size()) {                  
                	 ArrayList<Integer> questionKeys = new ArrayList<>(examPaper.getQuestions().keySet());

                	
                	int last1 = last; 
                	Question question = questions.get(questionKeys.get(last1));
                     if (question != null && question.judge(userAnswers.get(last))) {
                         prints.append(score).append(" ");
                     } else {
                         prints.append("0 ");
                     }
                 } else {
                     
                     prints.append("0 ");   
                 }
                 last++;
             }
             
             if (prints.length() > 0) {
            	 prints.deleteCharAt(prints.length() - 1);}
             prints.append("~").append(total);

             System.out.println(prints.toString());
         }
  
    }
        
    }


  
    

类图

image

踩坑心得

因为这次的代码难度有所提升,因此没有满分,但是还是得了大部分分,踩了一部分坑,调试了很久

1.没有想清楚类之间的关系,因此设计的时候很乱。

2.没有完全符合单一变量职责。

3.输出信息的顺序不对,导致输出混乱。

4.没有提前画类图构思,而是写一步想一步。

5.没有加入新的类,而是对类进行增加,不符合开闭原则

改进建议

1.应增加几个类,实现不同的职责

2.在设计类的时候应提前做类图,有设计的方向

3.在设计类的时候先明白面向的对象的目的,了解类之间的关系

4.此代码没解决输入错误的问题,因此显示非零返回,应判断数据是否为null,是否越界

PTA第三次作业最后一题

7-3 答题判题程序-3
分数 80
作者 蔡轲
单位 南昌航空大学
设计实现答题程序,模拟一个小型的测试,以下粗体字显示的是在答题判题程序-2基础上增补或者修改的内容,要求输入题目信息、试卷信息、答题信息、学生信息、删除题目信息,根据输入题目信息中的标准答案判断答题的结果。

输入格式:

程序输入信息分五种,信息可能会打乱顺序混合输入。

1、题目信息
题目信息为独行输入,一行为一道题,多道题可分多行输入。

格式:"#N:"+题目编号+" "+"#Q:"+题目内容+" "#A:"+标准答案

格式约束:
1、题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。
2、允许题目编号有缺失,例如:所有输入的题号为1、2、5,缺少其中的3号题。此种情况视为正常。
样例:#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4

2、试卷信息

试卷信息为独行输入,一行为一张试卷,多张卷可分多行输入数据。
格式:"#T:"+试卷号+" "+题目编号+"-"+题目分值+" "+题目编号+"-"+题目分值+...

格式约束:
题目编号应与题目信息中的编号对应。
一行信息中可有多项题目编号与分值。
样例:#T:1 3-5 4-8 5-2

3、学生信息

学生信息只输入一行,一行中包括所有学生的信息,每个学生的信息包括学号和姓名,格式如下。

格式:"#X:"+学号+" "+姓名+"-"+学号+" "+姓名....+"-"+学号+" "+姓名

格式约束:
答案数量可以不等于试卷信息中题目的数量,没有答案的题目计0分,多余的答案直接忽略,答案之间以英文空格分隔。
样例:

#S:1 #A:5 #A:22
1是试卷号
5是1号试卷的顺序第1题的题目答案
4、答卷信息

答卷信息按行输入,每一行为一张答卷的答案,每组答案包含某个试卷信息中的题目的解题答案,答案的顺序号与试 卷信息中的题目顺序相对应。答卷中:

格式:"#S:"+试卷号+" "+学号+" "+"#A:"+试卷题目的顺序号+"-"+答案内容+...

格式约束:
答案数量可以不等于试卷信息中题目的数量,没有答案的题目计0分,多余的答案直接忽略,答案之间以英文空格分隔。
答案内容可以为空,即””。
答案内容中如果首尾有多余的空格,应去除后再进行判断。
样例:
#T:1 1-5 3-2 2-5 6-9 4-10 7-3
#S:1 20201103 #A:2-5 #A:6-4
1是试卷号
20201103是学号
2-5中的2是试卷中顺序号,5是试卷第2题的答案,即T中3-2的答案
6-4中的6是试卷中顺序号,4是试卷第6题的答案,即T中7-3的答案
注意:不要混淆顺序号与题号

5、删除题目信息

删除题目信息为独行输入,每一行为一条删除信息,多条删除信息可分多行输入。该信息用于删除一道题目信息,题目被删除之后,引用该题目的试卷依然有效,但被删除的题目将以0分计,同时在输出答案时,题目内容与答案改为一条失效提示,例如:”the question 2 invalid~0”

格式:"#D:N-"+题目号

格式约束:

   题目号与第一项”题目信息”中的题号相对应,不是试卷中的题目顺序号。

   本题暂不考虑删除的题号不存在的情况。      

样例:

#N:1 #Q:1+1= #A:2

#N:2 #Q:2+2= #A:4

#T:1 1-5 2-8

#X:20201103 Tom-20201104 Jack

#S:1 20201103 #A:1-5 #A:2-4

#D:N-2 end

输出
alert: full score of test paper1 is not 100 points
1+1=5false
the question 2 invalid~0
20201103 Tom: 0 0~0
答题信息以一行"end"标记结束,"end"之后的信息忽略。

输出格式:

1、试卷总分警示

该部分仅当一张试卷的总分分值不等于100分时作提示之用,试卷依然属于正常试卷,可用于后面的答题。如果总分等于100 分,该部分忽略,不输出。

格式:"alert: full score of test paper"+试卷号+" is not 100 points"

样例:alert: full score of test paper2 is not 100 points

2、答卷信息

一行为一道题的答题信息,根据试卷的题目的数量输出多行数据。

格式:题目内容+""+答案++""+判题结果(true/false)

约束:如果输入的答案信息少于试卷的题目数量,答案的题目要输"answer is null"
样例:
3+2=5true
4+6=22false.
answer is null

3、判分信息

判分信息为一行数据,是一条答题记录所对应试卷的每道小题的计分以及总分,计分输出的先后顺序与题目题号相对应。

格式:**学号+" "+姓名+": "**+题目得分+" "+....+题目得分+"~"+总分

格式约束:

 1、没有输入答案的题目、被删除的题目、答案错误的题目计0分
 2、判题信息的顺序与输入答题信息中的顺序相同
样例:20201103 Tom: 0 0~0

   根据输入的答卷的数量以上2、3项答卷信息与判分信息将重复输出。

4、被删除的题目提示信息

当某题目被试卷引用,同时被删除时,答案中输出提示信息。样例见第5种输入信息“删除题目信息”。

5、题目引用错误提示信息

试卷错误地引用了一道不存在题号的试题,在输出学生答案时,提示”non-existent question~”加答案。例如:

输入:

#N:1 #Q:1+1= #A:2

#T:1 3-8

#X:20201103 Tom-20201104 Jack-20201105 Www

#S:1 20201103 #A:1-4 end

输出:
alert: full score of test paper1 is not 100 points
non-existent question~0
20201103 Tom: 0~0
如果答案输出时,一道题目同时出现答案不存在、引用错误题号、题目被删除,只提示一种信息,答案不存在的优先级最高,例如:
输入:

#N:1 #Q:1+1= #A:2

#T:1 3-8

#X:20201103 Tom-20201104 Jack-20201105 Www

#S:1 20201103 end

输出:
alert: full score of test paper1 is not 100 points
answer is null
20201103 Tom: 0~0

6、格式错误提示信息

输入信息只要不符合格式要求,均输出”wrong format:”+信息内容。

  例如:wrong format:2 #Q:2+2= #4

7、试卷号引用错误提示输出

如果答卷信息中试卷的编号找不到,则输出”the test paper number does not exist”,答卷中的答案不用输出,参见样例8。

8、学号引用错误提示信息

如果答卷中的学号信息不在学生列表中,答案照常输出,判分时提示错误。参见样例9。

本题暂不考虑出现多张答卷的信息的情况。

输入样例1:
简单输入,不含删除题目信息。例如:

#N:1 #Q:1+1= #A:2

#T:1 1-5

#X:20201103 Tom

#S:1 20201103 #A:1-5 end 输出样例1: 在这里给出相应的输出。例如:

alert: full score of test paper1 is not 100 points
1+1=5false
20201103 Tom: 0~0
输入样例2:
简单输入,答卷中含多余题目信息(忽略不计)。例如:

#N:1 #Q:1+1= #A:2

#T:1 1-5

#X:20201103 Tom

#S:1 20201103 #A:1-2 #A:2-3 end 输出样例3 简单测试,含删除题目信息。例如:

alert: full score of test paper1 is not 100 points
1+1=2true
20201103 Tom: 5~5
输入样例3:
简单测试,含删除题目信息。例如:

#N:1 #Q:1+1= #A:2

#N:2 #Q:2+2= #A:4

#T:1 1-5 2-8

#X:20201103 Tom-20201104 Jack-20201105 Www

#S:1 20201103 #A:1-5 #A:2-4

#D:N-2 end 输出样例3: 在这里给出相应的输出,第二题由于被删除,输出题目失效提示。例如:

alert: full score of test paper1 is not 100 points
1+1=5false
the question 2 invalid~0
20201103 Tom: 0 0~0
输入样例4:
简单测试,含试卷无效题目的引用信息以及删除题目信息(由于题目本身无效,忽略)。例如:

#N:1 #Q:1+1= #A:2

#N:2 #Q:2+2= #A:4

#T:1 1-5 3-8

#X:20201103 Tom-20201104 Jack-20201105 Www

#S:1 20201103 #A:1-5 #A:2-4

#D:N-2 end 输出样例4: 输出不存在的题目提示信息。例如:

alert: full score of test paper1 is not 100 points
1+1=5false
non-existent question~0
20201103 Tom: 0 0~0
输入样例5:
综合测试,含错误格式输入、有效删除以及无效题目引用信息。例如:

#N:1 +1= #A:2

#N:2 #Q:2+2= #A:4

#T:1 1-5 2-8

#X:20201103 Tom-20201104 Jack-20201105 Www

#S:1 20201103 #A:1-5 #A:2-4 #D:N-2 end 输出样例5: 在这里给出相应的输出。例如:

wrong format:#N:1 +1= #A:2
alert: full score of test paper1 is not 100 points
non-existent question~0
the question 2 invalid~0
20201103 Tom: 0 0~0
输入样例6:
综合测试,含错误格式输入、有效删除、无效题目引用信息以及答案没有输入的情况。例如:

#N:1 +1= #A:2

#N:2 #Q:2+2= #A:4

#T:1 1-5 2-8

#X:20201103 Tom-20201104 Jack-20201105 Www

#S:1 20201103 #A:1-5

#D:N-2 end 输出样例6: 答案没有输入的优先级最高。例如:

wrong format:#N:1 +1= #A:2
alert: full score of test paper1 is not 100 points
non-existent question~0
answer is null
20201103 Tom: 0 0~0
输入样例7:
综合测试,正常输入,含删除信息。例如:

#N:2 #Q:2+2= #A:4

#N:1 #Q:1+1= #A:2

#T:1 1-5 2-8

#X:20201103 Tom-20201104 Jack-20201105 Www

#S:1 20201103 #A:2-4 #A:1-5

#D:N-2 end 输出样例7: 例如:

alert: full score of test paper1 is not 100 points
1+1=5false
the question 2 invalid~0
20201103 Tom: 0 0~0
输入样例8:
综合测试,无效的试卷引用。例如:

#N:1 #Q:1+1= #A:2

#T:1 1-5

#X:20201103 Tom

#S:2 20201103 #A:1-5 #A:2-4 end 输出样例8: 例如:

alert: full score of test paper1 is not 100 points
The test paper number does not exist
输入样例9:
无效的学号引用。例如:

#N:1 #Q:1+1= #A:2

#T:1 1-5

#X:20201106 Tom

#S:1 20201103 #A:1-5 #A:2-4 end 输出样例9: 答案照常输出,判分时提示错误。例如:

alert: full score of test paper1 is not 100 points
1+1=5false
20201103 not found

输入样例10:
信息可打乱顺序输入:序号不是按大小排列,各类信息交错输入。但本题不考虑引用的题目在被引用的信息之后出现的情况(如试卷引用的所有题目应该在试卷信息之前输入),所有引用的数据应该在被引用的信息之前给出。例如:

#N:3 #Q:中国第一颗原子弹的爆炸时间 #A:1964.10.16

#N:1 #Q:1+1= #A:2

#X:20201103 Tom-20201104 Jack-20201105 Www

#T:1 1-5 3-8

#N:2 #Q:2+2= #A:4

#S:1 20201103 #A:1-5 #A:2-4 end 输出样例10: 答案按试卷中的题目顺序输出。例如:

alert: full score of test paper1 is not 100 points
1+1=5false
中国第一颗原子弹的爆炸时间4false
20201103 Tom: 0 0~0
代码长度限制
30 KB
时间限制
1500 ms
内存限制
64 MB
栈限制
8192 KB

源代码

点击查看代码
import java.util.*;
import java.util.Scanner;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Question {
    private int num;
    private String content;
    private String standardAnswer;
    public Question(int num, String content, String standardAnswer) {
        this.num = num;
        this.content = content;
        this. standardAnswer = standardAnswer;
    }
    public int getNum() {
        return num;
    }
    public String getContent() {
        return content;
    }
     public String getStandardAnswer() {
        return standardAnswer;
    }
    public boolean judge(String answer) {
       // return this.standardAnswer.trim().equalsIgnoreCase(answer.trim());
           return this.standardAnswer.equals(answer);
    }
}
class ExamPaper {
    private int num;
    private Map<Integer, Integer> questionscore = new LinkedHashMap<>();
 //   private static Map<Integer,Question> questions=new HashMap<>();
    public void addQuestion(int questionid ,int score) {
        questionscore.put(questionid, score);
    }
    public ExamPaper(int num)
    {
        this.num=num;
    }
    
    public Map<Integer,Integer> getQuestions() {
        return questionscore;
    }

    public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getTotalScore() {  
	    int total = 0;  
	    for (int score : questionscore.values()) {  
	        total += score;  
	    }  
	    return total;  
	}
}

class ExamAnswer {
	  private int paperId;
	  private List<String> answers = new ArrayList<>();
	  private String studentId;
	  private int questionOrder;

	  public ExamAnswer(int paperId, String studentId, int questionOrder, List<String> answers) {
	    this.paperId = paperId;
	    this.answers = answers;
	    this.questionOrder = questionOrder;
	    this.studentId = studentId;
	  }
        
	  public int getQuestionOrder() {
	    return questionOrder;
	  }

	  public int getPaperId() {
	    return paperId;
	  }

	  public List<String> getAnswers() {
	    return answers;
	  }

	  public String getStudentId() {
	    return studentId;
	  }
	}

	class Student {
	  private String id;
	  private String name;

	  public Student(String id, String name) {
	    this.id = id;
	    this.name = name;
	  }

	  public String getId() {
	    return id;
	  }

	  public String getName() {
	    return name;
	  }
	}

	class DeleteQuestion {
	  private static List<DeleteQuestion> deletedQuestions = new ArrayList<>();
	  private int questionId;

	  public DeleteQuestion(int questionId) {
	    this.questionId = questionId;
	  }
      public static void updata(int questionId, ExamPaper examPaper) {
       // examPaper.getQuestions().remove(questionId);
          examPaper.getQuestions().put(questionId, 0);
    }
	  public int getQuestionId() {
	    return questionId;
	  }

	  public static void deleteQuestion(int questionId) {
	    deletedQuestions.add(new DeleteQuestion(questionId));
	  }

	  public static boolean isQuestionDeleted(int questionId) {
	    for (DeleteQuestion dq : deletedQuestions) {
	      if (dq.getQuestionId() == questionId) {
	        return true;
	      }
	    }
	    return false;
	  }

	  public static void printDeletedQuestion(Question question) {
	    System.out.println("the question " + question.getNum() + " invalid~0");
	  }
	}

    class AnswerWithOrder {  
    int questionOrder;  
    String answer;  
  
    public AnswerWithOrder(int questionOrder, String answer) {  
        this.questionOrder = questionOrder;  
        this.answer = answer;  
    }  
  
    
    public int getQuestionOrder() {  
        return questionOrder;  
    }  
  
    public String getAnswer() {  
        return answer;  }
    }

   class Regex{
  

//#N
           public static boolean matchN(String line) {  
        String regex = "#N:(\\d{1}) #Q:(\\d{1})\\+(\\d{1})= #A:(\\d{1})";  
        Pattern pattern = Pattern.compile(regex);  
        Matcher matcher = pattern.matcher(line);  
        return matcher.matches();  
    }  
//#T
       public static boolean matchT(String line) {  
       // String regex = "#T:(\\d{1}) (\\d{1})-(.*)$";  
              String regex = "^#T:\\d+( \\d+-\\d+)*";
        Pattern pattern = Pattern.compile(regex);  
        Matcher matcher = pattern.matcher(line);  
        return matcher.matches(); }
           
//#S:
    public static boolean matchS(String line) {  
        String regex = "^#S:\\d+\\s+\\d{8}(\\s+#A:\\d+-\\d+)*$";  
        Pattern pattern = Pattern.compile(regex);  
        Matcher matcher = pattern.matcher(line);  
        return matcher.matches();  
    }  
  
//#X: 
    public static boolean matchX(String line) {  
        //String regex = "^#X:(\\d+\\s+[a-zA-Z]+-)";  
       String regex = "(\\s*#X:\\s*){1}([0-9]+\\s+[^- ]+-){0,}([0-9]+\\s+[^- ]+){1}\\s*";
        Pattern pattern = Pattern.compile(regex);  
        Matcher matcher = pattern.matcher(line);  
        return matcher.matches();  
    }  
  
//#D  
    public static boolean matchD(String line) {  
        String regex = "^#D:N-\\d+";  
        Pattern pattern = Pattern.compile(regex);  
        Matcher matcher = pattern.matcher(line);  
        return matcher.matches();  
    }  
   }

public class Main {
	private static Map<Integer, Question> questions = new HashMap<>();
	  private static Map<Integer, ExamPaper> examPapers = new HashMap<>();
	  private static List<ExamAnswer> ExamAnswers = new ArrayList<>();
	  private static List<Student> students = new ArrayList<>();
	  
    public static void main(String[] args) {
    	Scanner scanner = new Scanner(System.in);
        Pattern pattern = Pattern.compile("#N:(\\d+) #Q:(.+) #A:(.+)");
       // Pattern pattern = 
        int studentflag1=0;
        int studentflag2=0;
        int studentflag3=0;
        int paperId1=0;
        while (scanner.hasNextLine()) {
          String line = scanner.nextLine().trim();
          if ("end".equals(line)) {
            break;
          }

          if (line.startsWith("#N:")) {
           
              if(!Regex.matchN(line)){
                  System.out.println("wrong format:" + line);
                  continue;
              }
              Matcher matcher = pattern.matcher(line);
            if (matcher.find()) {
              int id = Integer.parseInt(matcher.group(1));
              String content = matcher.group(2).trim();
              String answer = matcher.group(3).trim();
              Question ques = new Question(id, content, answer);
              questions.put(id, ques);
                
            }
          } 
          
          else if(Regex.matchT(line)){
                 
                  
              
            String[] parts = line.split(" ");
            int paperId = Integer.parseInt(parts[0].substring(3));
             paperId1=paperId;
            ExamPaper examPaper = new ExamPaper(paperId);
            for (int i = 1; i < parts.length; i++) {
              String[] questionInfo = parts[i].split("-");
              int questionId = Integer.parseInt(questionInfo[0]);
              int score = Integer.parseInt(questionInfo[1]);
              examPaper.addQuestion(questionId, score);
            }
            examPapers.put(paperId, examPaper);}
      
           
              else  if(Regex.matchX(line)){
            
               studentflag1=1;
            String[] parts = line.substring(3).split("-");
          //  List<Student> students = new ArrayList<>();
            for (String part : parts) {
                String[] info = part.trim().split(" ");
                if (info.length == 2) {
                    String id = info[0];
                    String name = info[1];
                    students.add(new Student(id, name));
                }
            }}
         
            else if(Regex.matchS(line)){
              
            String[] parts = line.split(" ");
            int paperId = Integer.parseInt(parts[0].substring(3));
           
            String studentId = parts[1];
            int questionOrder = 0;
            List<String> answers = new ArrayList<>();
              List<AnswerWithOrder> answersWithOrder=new ArrayList<>();
              
            for (int i = 2; i < parts.length; i++) {
              String[] answerParts = parts[i].split("-");
              questionOrder = Integer.parseInt(answerParts[0].substring(3));
             // String answerd = answerParts[1];
                String answer = answerParts[1];
            //  answers.add(answerd);
                    answersWithOrder.add(new AnswerWithOrder(questionOrder,answer));
            }
              answersWithOrder.sort(Comparator.comparingInt(AnswerWithOrder::getQuestionOrder));  
                  

    for (AnswerWithOrder lastlist1 : answersWithOrder) {  
        answers.add(lastlist1.getAnswer());  
    }  
            ExamAnswers.add(new ExamAnswer(paperId, studentId, questionOrder, answers));
          } 
            
               else   if(Regex.matchD(line)){
             
            int questionId = Integer.parseInt(line.substring(5));
            DeleteQuestion.deleteQuestion(questionId);
          } 
             else {
             System.out.println("Wrong Format:" + line);
           }
        }
    //System.out.println("a");//测试
        for (ExamAnswer examAnswer : ExamAnswers) {  
          //  for(Map<Integer,ExamPaper> exampaper :examPapers){
            ExamPaper examPaper = examPapers.get(examAnswer.getPaperId());  
          
          if (examPaper== null || examPaper.getTotalScore() != 100) {
        //   if ( examPaper!= null &&examPaper.getTotalScore() != 100) {  
                System.out.println("alert: full score of test paper" + paperId1+ " is not 100 points");  
                
          //  }  
        }
    }
     
        for(ExamAnswer examAnswer: ExamAnswers) {
        	ExamPaper examPaper= examPapers.get(examAnswer.getPaperId()); 
        
        	 List<String> userAnswers = examAnswer.getAnswers();
        	int total=0;
        	int i=0;
            if(examPaper ==null) {
                System.out.println("The test paper number does not exist");
            }
      
        	else{ for (Map.Entry<Integer, Integer> entry : examPaper.getQuestions().entrySet()) {
                //entry是exampaper的问题分数map映射 examPaper.getQuestions().values()
                 //entry.getKey()是#T的试卷号/问题id
               //  System.out.println(entry.getKey());//用于测试
                 Question question = questions.get(entry.getKey());
    //                 if (question == null) {
    //             System.out.println("non-existent question~" +0) ;//userAnswers.get(i));
    //                 i++;
    //                 continue;
    // }
                 String userAnswer;  
                 int flag1=0;
                 if (i < userAnswers.size()) {  
                     userAnswer = userAnswers.get(i);  
                 } else {   
                      total += 0;
                    System.out.println("answer is null");
                        i++;
                    continue;
                 }
               //  System.out.println(question);//用于测试
                  // // 删除
                 int deleteflag=0;
                 if (DeleteQuestion.isQuestionDeleted(entry.getKey())) {
                   DeleteQuestion.printDeletedQuestion(question);
                   DeleteQuestion.updata(entry.getKey(),examPaper);
    //                                  if (question == null) {
    //             System.out.println("non-existent question~" +0) ;//userAnswers.get(i));
    //                 i++;
    //                 continue;
    // }
                     deleteflag=1;
                   i++;
                     
                   continue;
                 }
                                                  if (question == null) {
                System.out.println("non-existent question~" +0) ;//userAnswers.get(i));
                    i++;
                    continue;
    }
                 if (question != null) {//1
                	 
                     boolean flag = question.judge(userAnswer);
                     if (flag) {
                         if(deleteflag==0){total += entry.getValue();}
                         if(deleteflag==0){total += 0;}
                         System.out.println(question.getContent() + "~" + userAnswer + "~true");
                     } else {
                     System.out.println(question.getContent() + "~" + userAnswer + "~false");
                     }
                         
                     }//1
                 } 
        
         
        	 for(Student student:students){
                 if(student.getId().equals(examAnswer.getStudentId())){
                     System.out.print(student.getId()+" "+student.getName()+": ");
                      studentflag3=1;
                 }
                 if(studentflag3==0) {
                     System.out.print(examAnswer.getStudentId()+" not found");
                studentflag2=1;
                 }
             }
             StringBuilder prints = new StringBuilder();
             int last = 0;
            if(!(studentflag1==1&&studentflag2==1)){
             for (Integer score : examPaper.getQuestions().values()) {
                 if (last < userAnswers.size()) {                  
                	 ArrayList<Integer> questionKeys = new ArrayList<>(examPaper.getQuestions().keySet());

                	
                	int last1 = last; 
                	Question question = questions.get(questionKeys.get(last1));
                     if (question != null && question.judge(userAnswers.get(last))) {
                         prints.append(score).append(" ");
                         
                     } else {
                         prints.append("0 ");
                     }
                 } else {
                     
                     prints.append("0 ");   
                 }
                 last++;
             }
             
             if (prints.length() > 0) {
            	 prints.deleteCharAt(prints.length() - 1);}
             prints.append("~").append(total);

             System.out.println(prints.toString());
         }
        }
    }
    }
    }


  
    

类图

image

这次是难度最大的一次,我只得了一半的分数

踩坑心得

1.还是那个问题,没能完全清楚类之间的关系,做出来的类可以说是毫无联系。

2.虽然添加很很多类,但是无法完全的单一职责。

3.很多的因为输出null,数组越界没有解决。

4.正则表达式有点小问题。

5.在上一次代码的基础上可以说是大改,而不是基于它来改。

6.类之间引用的时候没有理清关系而导致引用错误。

7.没有边写边注释,在下一次看的时候要重新思考。

8.在主类中没有设置方法,而是直接裸出代码,导致代码很乱,没有规则性。

改进建议

1.最基本的问题,边写代码边注释,方便自己也方便他人。

2.在写代码之前理清面向对象的方向的目标,做出类图。

3.在报错的时候理性分析,之前因为一个很简单的非零返回卡了三天。

4.多学习和使用正则表达式,很方便但容易错,很多的测试点因为正则表达式而没过。

5.再增加几个类,实现单一职责。

6.没实现乱序输入。

总结

经过这三次的PTA作业,让我学到很多东西,收获了很多道理。

1.对Java的学习更加深入,对语法更加熟悉。

2.在这次的PTA作业中,遇到了很多的问题,这些问题一眼来很难解决,但是必须要解决;要培养自己解决问题的能力,无论遇到什么困难都要自己一点一点的克服。

3.在遇到问题时要提前解决,不能拖时间,虽然这次我很早就做PTA了还是没有做到满意的成都,可见要是拖到最后的后果。

4.要独立自主学习,提前在网上学习要用到的的知识点,才能顺利完成自己的问题。

5.在写代码之前要完成类图,做出自己的设计,不能边做边设计,否则可能做到一半就和面向的对象要求不符合。 <6>6.思考问题要全面,不能思考眼前的问题,要思考大局。

7.在学习态度上没有完全端正,很多的时候做好目标自己就怠惰了,在今后要解决这种问题。

8.在映射方面的学习程度要加深,在写代码的时候我深深了解到它的重要性,它能实现很多的需求。

在这轮的PTA作业中,我受益匪浅,在面向对象的方向上学习了很多,了解了类与对象,各种的原则,在下次的PTA作业中会继续学习继承与多态,希望在今后的学习中能掌握和使用继承和多态做程序的设计。