针对PTA题集1~3最后一题的总结

阅读目录

前言

第一次PTA

第二次PTA

第三次PTA

总结

前言

一:对于java的认识

java这门语言对于我来说是一门陌生的语言,开始学习起来是比较吃力的,上学期只接触了C语言,虽然C预言是比较基础的,可以让你在接触到新的一门语言是更加容易上手,
但是java本身是有一定难度的,这就导致了我在做java的一些作业时会出现许多问题。

二:对于三次PTA的理解

1.第一次PTA的作业是我们对java的第一次实练,所以难度相比后面的几次就显的比较小,但是对于本人来说还是具有一定难度才完成的。
2.第二次PTA的作业就开始变得有挑战性,需要思考的更多了,因为这次没有留出足够的时间去思考,所以导致的结果就是没有完成所有的难题,这就导致了后面一些问题的出现。
3.第三次PTA的作业这次虽然留出了足够的时间去思考但是因为上一次的作业并没有全部完成,就导致了更多的问题的出现,需要思考的问题也就更多了,这也就导致了这次的作业也未完成。

第一次PTA

设计与分析

这次作业的设计很简单,设计如下:
Text类:

主类Main
分析代码
此次作业思考欠佳,导致了后面的很多问题需要重新思考,只使用了一个类就完成了编译。
power designer输出例如:

由此可以看出,关系之间的关系是非常简单的;
下面是SourceMonitor的测试结果:

通过测试可以看出十分简单;
而代码的核心如下图所示:

这张图是将输入内容存储入数组;

而这张图就展示了将数组中的信息传到类中,然后在判断的核心内容。

踩坑心得

因为这是第一次的PTA昨天所以刚开始写的还是很吃力的,特别是正则表达式的运用十分不熟悉,经过多次修改才最后通过了这次的作业

改进建议

1.因为这是第一次PTA作业(java)所以很多方面考虑不完全,就比如这道题应该使用至少三个类来编写才是正确的解决方法,但是当时本人思考的是一道题它的解法有很多种,每个人都有自己的想法,所以只要能解出来的方法就是好方法,但是我忽略了一件事,那就是之后的PTA发布都会是在之前的基础上迭代的,会越来越难,这就导致我如果继续按照这样的写法就会很麻烦因为几乎每一道题都需要重新思考。
2.所以根据前一点的总结,这道题应该严格按照题目给的建议来进行编写,例如:建三个类,Paper将Answer和Topic关联,输出等方法也是放在了Paper类中,这样会更好。

第二次PTA

设计与分析

第二次的PTA就有了明显的难度提升而设计的类如下:
试卷类TextTopicScore

题目类QuestionAnswer

答卷类StudentPaper

关联类Judgment

主类Main
分析代码
此次的作业因为有了第一次的原因所以在一些方面会有更多的理解
power designer输出例如:

可以很明显的看出复杂程度大大高于第一题的复杂程度
下面是SourceMonitor的测试结果:

仔细看是可以看出相对于第一题在一些数值上是更大了的
而这一次的读取输入就比较复杂了出了第一次使用的正则表达式还运用了spilt,他们两个相辅相成
代码如下:

点击查看代码
       while(true){
            a=-1;
            all = sc.nextLine();
            if(all.equals("end"))
                break;
            if(all.charAt(1) == 'N'){
                Pattern pattern1 = Pattern.compile("#N:\\s*(\\d+) #Q\\s*:(.+) #A:\\s*(.+)");
                Matcher matcher = pattern1.matcher(all);
                if (matcher.find()){
                    m[num1] = Integer.parseInt(matcher.group(1).trim());
                    question[num1] = matcher.group(2).trim();
                    answer1[num1] = matcher.group(3).trim();
                }
                num1++;
                questionAnswer.setQuestion(question);
                questionAnswer.setAnswer(answer1);
                questionAnswer.setNum(m);
            }
            if(all.charAt(1) == 'T'){
                String[] textPaper = all.split("#T:| |-");
                num2 = Integer.parseInt(textPaper[1]);
                for(int i = 2; i < textPaper.length; i++){
                    if(i%2==1){
                        score1[j] = Integer.parseInt(textPaper[i]);
                        j++;
                    }
                    if(i%2==0){
                        topic1[k] = Integer.parseInt(textPaper[i]);
                        k++;
                    }
                }
                score2.setTopic(topic1);
                score2.setScore(score1);
                score2.setNum(num2);
                score2.allScore();
            }
            if(all.charAt(1) == 'S'){
                String[] studentPaper = all.split("#S:| #A:");
                num3 = Integer.parseInt(studentPaper[1]);
                for(int i = 2; i < studentPaper.length; i++){
                    studentAnswer1[l] = studentPaper[i];
                    l++;
                }
                studentText.setNum(num3);
                studentText.setStudentAnswer(studentAnswer1);
                if(num3 != 0){
                    Judgment endJudgment = new Judgment(questionAnswer,score2,studentText);
                    a = endJudgment.judgmentText();
                    if(a == 1){
                        endJudgment.judgmentAnswer();
                    }
                    else{
                        continue;
                }
            }
            }
        }

!!!注意根据最后的结果显示,这样写,判断是有问题的,只能解决部分输入的问题,仍有一些格式的输入是无法被解决的。!!!
而判断和输出的核心代码如下:

点击查看代码
    public void allScore(){
        int Score=0;
        for(int i = 0;i < score.length;i++){
            Score = Score + score[i];
        }
        if(Score != 100){
            System.out.println("alert: full score of test paper" + num + " is not 100 points");
        }
    }

这个是输出试卷不满100分的判断;

点击查看代码
    public int judgmentText(){
        if(studentPaper.getNum() == textTopicScore.getNum()){
            return 1;
        }
        else{
            System.out.println("The test paper number does not exist");
            return 0;
        }
    }

这个是输出错误试卷或答卷号的;

点击查看代码
        int topic,x = -1,score=0,s=0;
        String str = "null";
        boolean result;
        for(int i = 0; i < textTopicScore.getNum1();i++){
            topic = textTopicScore.getTopic()[i];
            for(int j = 0; j < questionAnswer.getNum().length;j++){
                if(topic == questionAnswer.getNum()[j]){
                    x = j;
                    break;
                }
            }
            if(studentPaper.getStudentAnswer()[i].equals(str)){
                System.out.println("answer is null");
            }
            else{
                if(questionAnswer.getAnswer()[x].equals(studentPaper.getStudentAnswer()[i])){
                    result = true;
                }
                else{
                    result = false;
                }
                System.out.println(questionAnswer.getQuestion()[x] + "~" + studentPaper.getStudentAnswer()[i] + "~" + result);
            }
        }
        for(int i = 0; i < textTopicScore.getNum1();i++){
            topic = textTopicScore.getTopic()[i];
            for(int j = 0; j < questionAnswer.getNum().length;j++){
                if(topic == questionAnswer.getNum()[j]){
                    x = j;
                    break;
                }
            }
            if(studentPaper.getStudentAnswer()[x] == "null"){
                
            }
            else{
                if(questionAnswer.getAnswer()[x].equals(studentPaper.getStudentAnswer()[i])){
                    result = true;
                    System.out.print(textTopicScore.getScore()[x]);
                    s = textTopicScore.getScore()[x];
                    score = score + textTopicScore.getScore()[x];
                }
                else{
                    result = false;
                    System.out.print("0");
                    score = score + 0;
                }
                if(textTopicScore.getNum1() == 1){
                    
                }
                else{
                    if(x==0)
                    System.out.print(" ");
                }
            }
        }
        System.out.println("~" + score);
    }

而这个就是输出和判断的核心代码。
!!!注意这里的判断和代码是不完全正确的,依旧有一些问题是没有解决!!!

踩坑心得

1.第二次的PTA作业因为输入变得更多了,例如加了#T,#S等,这就导致正则表达式需要更多种,也就导致了出现了很多编译上的问题,而这里我为了避免因为对正则表达式不熟悉而产生一些问题,我将部分需要用到正则表达式的判断改为了split,这样存储就变得比较容易理解。
2.因为第一次PTA作业的原因,就导致了这次的作业基本上自己重新开始写的,然而还因为自己留出的时间不充足,思考不充分,就例如固定的思考为输入到#S可以理解为一套试卷,就忘记了可以乱序输入的情况,就会是输出是有很大问题。
3.因为前一点的问题就导致了在存储数据是会存在数据不完全或重复存入数据的情况,等等一些不可避免的大大小小的问题。
4.因为时间问题思考的很仓促,就开始写题,导致写的很不顺畅而且会有很多地方是与自己想的有出入的,很多时候自己想的很简单,而实现起来会很麻烦很多问题。

改进建议

1.在下一次作业开始后留出足够的时间。
2.再开始编译前先画类图提前将自己的思路开放出来。
3.下一次作业在读入数据时先将所有的数据存入到数组中然后最后在再全部传进关联类进行判断

第三次PTA

设计与分析

而第三次的作业就更加复杂更加完善
类的设计如下:
问题类Question

学生类Student

学生和其答卷类StudentAnswer

试卷类Text

删除类Delete

数量类allKindNum

关联判断类allJudgment

主类Main
这一次的作业就显得完善了所以也就意味着更难了
power designer输出例如:

可以看出相比于第二次的作业就更加复杂了,关联的也要跟清楚了但也更多了;
下面是SourceMonitor的测试结果:

比较二三次作业的数据就会发现虽然题目改动不是很大但是,所编译出的代码却更加复杂了
这一次沿用了第二次作业的思路继续正则表达式和spilt的一起运用,但是此次的代码就要更加容易理解
输入读取代码如下:

点击查看代码
        while(true){
            String all = sc.nextLine();
            if(all.equals("end"))
                break;
            if(all.charAt(1) == 'N'){
                String[] textDelete = all.split("#N:| ");
                int c = Integer.parseInt(textDelete[1]);
                String regex = ("^#N:\\s*(\\d+) #Q\\s*:(.+) #A:\\s*(.+)");
                if(all.matches(regex)){
                    Pattern pattern1 = Pattern.compile(regex);
                    Matcher matcher = pattern1.matcher(all);
                    if (matcher.find()){
                        m = Integer.parseInt(matcher.group(1).trim());
                        question = matcher.group(2).trim();
                        answer1 = matcher.group(3).trim();
                        num1++;
                    }
                    questions[num1].setNum(m);
                    questions[num1].setQuestion(question);
                    questions[num1].setAnswer(answer1);
                }
                else{
                    System.out.println("wrong format:" + all);
                    questions[num1].setErrorNum(c);
                }
            }
            if(all.charAt(1) == 'T'){
                int score = 0;
                String[] textPaper = all.split("#T:| |-");
                m = Integer.parseInt(textPaper[1]);
                for(int i = 2; i < textPaper.length; i++){
                    if(i % 2 == 1){
                        score1[j] = Integer.parseInt(textPaper[i]);
                        score += score1[j];
                        j++;
                    }
                    if(i % 2 == 0){
                        topic1[k] = Integer.parseInt(textPaper[i]);
                        k++;
                    }
                }
                if(score != 100){
                    System.out.println("alert: full score of test paper" + m + " is not 100 points");
                }
                text[num2].setTextNum(m);
                text[num2].setQuestionNum(topic1);
                text[num2].setQuestionScore(score1);
                num2++;
            }
            if(all.charAt(1) == 'D'){
                String[] textDelete = all.split("#D:N-| |N-");
                m = Integer.parseInt(textDelete[1]);
                delete[num3].setQuestionNum(m);
                num3++;
            }
            if(all.charAt(1) == 'X'){
                String[] studentID = all.split("#X:| |-");
                for(int i = 1; i < studentID.length; i = i + 2){
                    String studentId = studentID[i];
                    String studentName = studentID[i+1];
                    students[num4].setId(studentId);
                    students[num4].setName(studentName);
                    num4++;
                }
            }
            if(all.charAt(1) == 'S') {
                String[] studentPaper = all.split("#S:| |#A:|-");
                m = Integer.parseInt(studentPaper[1]);
                String ID = studentPaper[2];
                for (int i = 4; i < studentPaper.length; i = i + 3) {
                    questionNum[l] = Integer.parseInt(studentPaper[i]);
                    questionAnswer[l] = studentPaper[i + 1];
                    l++;
                }
                studentAnswers[num5].setNum(m);
                studentAnswers[num5].setId(ID);
                studentAnswers[num5].setQuestionNum(questionNum);
                studentAnswers[num5].setQuestionAnswer(questionAnswer);
                num5++;
            }
        }

这就是对读取的数据做到存储处理和一些简单的判断例如:对试卷是否为100分的判断;
!!!解决了第二次作业的一些问题,但是又出现了一些第三次作业的问题,代码为不完全正确代码。!!!
而以下的是判断和输出的核心代码:

点击查看代码
    public void studentAndTextJudgment(){
        int x = 0;
        for(int i = 0; i < kindNum.getNum()[4]; i++){
            x = 0;
            for(int j = 0; j < kindNum.getNum()[1]; j++){
                if(studentAnswer[j].getNum() != text[i].getTextNum()){
                    x++;
                }
            }
            if(x == kindNum.getNum()[1]){
                System.out.println("The test paper number does not exist");
                return;
            }
        }
    }

这段代码是对试卷和答卷编号是否相同判断的代码;

点击查看代码
    public int questionErrorJudgment(int questionNum){
        for(int i = 0; i < kindNum.getNum()[2]; i++){
            if(question[i].getErrorNum() == questionNum){
                System.out.println("non-existent question~0");
                return 0;
            }
        }
        return 1;
    }

这段是判断输入的#N是否符合格式的代码;

点击查看代码
    public int questionAndDeleteJudgment(int questionNum){
        int n = 0;
        for(int i = 0; i < kindNum.getNum()[2]; i++){
            if(delete[i].getQuestionNum() == questionNum){
                System.out.println("the question " + questionNum + " invalid~0");
                return 0;
            }
        }
        return 1;
    }

这段是判断所答题目是否被删除的代码;

点击查看代码
    public int questionAndTextJudgment(int questionNum, int textNum){
        int n = 0;
        for(int i = 0; i < studentAnswer[textNum-1].getaAllQuestionNum(); i++){
            if(studentAnswer[textNum-1].getQuestionNum()[i] != questionNum){
                n++;
            }
        }
        int m = 0;
        for(int i = 0; i < kindNum.getNum()[0]; i++){
            if(question[i + 1].getNum() != questionNum){
                m++;
            }
        }
        if(n == studentAnswer[textNum-1].getaAllQuestionNum() && m == kindNum.getNum()[0]){
            System.out.println("non-existent question~0");
            return 0;
        }
        return 1;
    }

这段是判断当答案是否存在,和如果答案存在是否错误引用题目号的代码;

点击查看代码
    public void studentAndAnswerJudgment(){
        int x = 0, y = 0, z = 0, n = 0, a = 0;
        int score = 0;
        for(int i = 0; i < kindNum.getNum()[1]; i++){
            for(int j = 0; j < kindNum.getNum()[4]; j++){
                if(studentAnswer[i].getNum() == text[j].getTextNum()){
                    int[] error = new int[text[i].getaAllQuestionNum()];
                    for(int k = 0; k < text[i].getaAllQuestionNum(); k++){
                        x = 0;
                        a = questionAndTextJudgment(text[i].getQuestionNum()[k],text[j].getTextNum());
                        for(int l = 0; l < studentAnswer[j].getaAllQuestionNum(); l++){
                            if(text[i].getQuestionNum()[k] == studentAnswer[j].getQuestionNum()[l]){
                                y = questionAndDeleteJudgment(text[i].getQuestionNum()[k]);
                                z = questionErrorJudgment(text[i].getQuestionNum()[k]);
                                if (y == 1&&z == 1) {
                                    for(int t = 0; t < kindNum.getNum()[0]; t++){
                                        if(question[t + 1].getNum() == text[i].getQuestionNum()[k]){
                                            System.out.print(question[t + 1].getQuestion() + "~" + studentAnswer[j].getQuestionAnswer()[l] + "~");
                                        }
                                    }
                                    if(studentAnswer[j].getQuestionAnswer()[l].equals(question[text[i].getQuestionNum()[k]].getAnswer())){
                                        System.out.println("true");
                                        score += text[i].getQuestionScore()[studentAnswer[j].getQuestionNum()[l]-1];
                                        error[text[i].getQuestionNum()[k] - 1] = 1;
                                    }
                                    else{
                                        System.out.println("false");
                                        error[text[i].getQuestionNum()[k] - 1] = 0;
                                        score += 0;
                                    }
                                }
                            }
                            else{
                                x++;
                            }
                        }
                        if(x == studentAnswer[j].getaAllQuestionNum()&&y == 1&&a == 1){
                            System.out.println("answer is null");
                            score += 0;
                        }
                    }
                    System.out.print(studentAnswer[j].getId() + " ");
                    for(n = 0; n < kindNum.getNum()[3]; n++){
                        if(studentAnswer[j].getId().equals(student[n].getId())){
                            System.out.print(student[n].getName() + ": ");
                            for(int m = 0; m < text[i].getaAllQuestionNum(); m++){
                                if((m + 1) == text[i].getaAllQuestionNum()){
                                    if(error[m] == 0){
                                        System.out.println("0~" + score);
                                    }
                                    else{
                                        System.out.println(text[i].getQuestionScore()[text[i].getQuestionNum()[m]-1] + "~" + score);
                                    }
                                }
                                else{
                                    if(error[m] == 0){
                                        System.out.print("0 ");
                                    }
                                    else{
                                        System.out.print(text[i].getQuestionScore()[text[i].getQuestionNum()[m]-1] + " ");
                                    }
                                }
                            }
                            break;
                        }
                    }
                    if(n == kindNum.getNum()[3]){
                        System.out.println("not found");
                    }
                }
            }
        }
    }

而这一段代码是整道题判断和输出的核心。
!!!以上代码皆为不完全正确代码仍有许多问题需要处理。!!!(好难!)

踩坑心得

1.虽然这次留出了大量的时间来思考和编译这次的作业,但是还是未能完美完成作业,仍有许多不足的地方,只能做到解决大部分前一次的问题和部分这次的问题,但依旧不能完成。
2.没有考虑到在#~之前是可以输入如何东西的,但是不会报输入格式错误,就导致了在这上面浪费了很多时间,最在和同学讨论是才恍然大悟,但是已经来不及修改,就错失了许多分,但至少可以解决这个问题了。
3.在判断那几个优先级时,一直无法理解什么叫没有答案的优先级最高,也浪费了很多时间,但是最后在同学的帮助下终于理解,并解决问题。
4.在做判断时,所运用的循环一直有问题会出现运行超时,或陷入死循环,这个点卡了我很久,最后是又建立了一个专门来存储各个由多少套的类才得以解决的。

改进建议

1.在写下一次的作业时一定要将前一起相关的题吃透,这会是你在这次的编译中更加如鱼得水,更加轻松。
2.提前开始最后一题的思考愈早愈好,有什么问题一定要和同学一起讨论,这并不是抄,这本来就是同学之间一起解决的问题,不能有问题不问自己想,可能会想通但是会很久。
3.下次再写时,在存入数据时可以将其个数一起存入方便之后循环的运用。

总结

1.java是具有一定难度的,不能再像之前学C语言时一样,漫不经心,java需要我们沉淀下来慢慢琢磨,慢慢推敲,吃透所有的知识点,以便以后更加复杂的运用。
2.三次题集的难度是有目共睹的,不能像第一次一样按照自己的想法随便编写,需要严格按照要求来,有自己的想法固然是好事,但一定是要在要求下才可以,用得好是锦上添花,否则就变成了不按照规则办事,适得其反;
3.如果遇见迭代的题时,一定要一步一步走踏实了,建好基础才可以在后面的题目中走的更远,爬的更高。
4.理论上来说一道题类建的越多就应该越简单,但是还是要根据题目来建立,不管任何盲目的建立只会使代码更加混乱。
5.类一定要履行单一职责原则这样会使代码更加清晰明了,不至于将自己写昏。

posted @ 2024-04-21 10:10  逍遥小熊  阅读(61)  评论(0)    收藏  举报