ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PTA习题集1~3总结

2022-04-10 01:33:11  阅读:182  来源: 互联网

标签:总结 String get int 习题集 +- System PTA str


一、前言

    第一次作业:第一次作业的难度对于刚刚进入新的java学习阶段的我们可以说难度非常友好了,只要认真将mooc上的网课听了之后做起来会觉得非常轻松,但是有一些小的细节问题还是要注意,百密终有一疏,也导致我在此次作业丢了几分。

    第二次作业:相较于第一次作业第二次作业的难度明显更高了一些,题目二的长度也让人望而生畏,但实际操作起来并不是特别复杂(可惜的是我没有好好写,学的有点太少了)。

    第三次作业:第三次作业的第二三题的难度对我来说是非常大了,并没有得到几分,最后也是看了同学的源码才清楚了其中的一些思路,但还是写不出来,还需要自己付出努力啊。

二、设计与分析

    题目2.7-2

    源码

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7         String num = in.nextLine();
  8         int flag=0;
  9         int n = 1;
 10         if(num.length()<11)
 11         {
 12             System.out.print("null data");
 13             flag = 1;
 14         }
 15         int count=0;
 16         for(int i=0; i<num.length();i++) 
 17         {
 18             if(num.charAt(i)==1)
 19                 count++;
 20             if(count==num.length())
 21             {
 22                 System.out.print("null data");
 23                 flag = 1;
 24              }
 25         }
 26         if(flag!=1)
 27         {
 28             int i,j=0;
 29             for(j =0;j < num.length(); j++)
 30             {
 31                 if(num.charAt(j)=='0')
 32                 {
 33                     break;
 34                 }
 35             }
 36             if(j>=num.length())
 37             {
 38                 System.out.print("null data");
 39                 return;
 40             }
 41             for( i =0 ;i < num.length(); i++)
 42             {
 43                 int h = 0;
 44                 if(num.charAt(i)=='0')
 45                 {
 46 
 47                         for(h = i;h<i+10;h++)
 48                         {
 49                             if(num.charAt(h)=='0')
 50                             {
 51                                 break;
 52                             }
 53                         }
 54                         if(h>=i+10)
 55                         {
 56                             System.out.print(n+":");
 57                             System.out.println("null data");
 58                             i = i+10;
 59                             n++;
 60                         }
 61                     if(num.charAt(i+10) != '1')
 62                     {
 63                         System.out.print(n+":");
 64                         System.out.println("validate error");
 65                         i=i+10;
 66                         n++;
 67                     }
 68                     else
 69                     {
 70                         for(int k =i+1; k<i+9 ;k++)
 71                         {
 72                             if(num.charAt(k)=='1')
 73                             {
 74                                 count++;
 75                             }
 76                         }
 77                     if(count%2==0)
 78                     {
 79                         if(num.charAt(i+9)!='1')
 80                         {
 81                             System.out.print(n+":");
 82                             System.out.println("parity check error");
 83                             i = i+10;
 84                             n++;
 85                         }
 86                         else
 87                         {
 88                             System.out.print(n+":");
 89                             for(int k =i+1; k<i+9 ;k++)
 90                             {
 91                                 System.out.print(num.charAt(k));
 92                             }
 93                             System.out.println();
 94                             i=i+10;
 95                             n++;
 96                         }
 97                     }
 98                     else if(count%2!=0)
 99                     {
100                         if(num.charAt(i+9)=='1')
101                         {
102                             System.out.print(n+":");
103                             System.out.println("parity check error");
104                             i = i+10;
105                             n++;
106                         }
107                         else
108                         {
109                             System.out.print(n+":");
110                             for(int k =i+1; k<i+9 ;k++)
111                             {
112                                 System.out.print(num.charAt(k));
113                             }
114                             System.out.println();
115                             i=i+10;
116                             n++;
117                             }
118                         }
119                     }
120                 }
121             }
122         }
123     }
124 }

 

注意:该题的if和else的嵌套循环非常多且非常复杂,但是只要弄清楚其中的结构也是非常简单的。

题目3.7-1

 1 import java.util.Scanner;
 2 import java.util.ArrayList;
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6          Scanner input = new Scanner(System.in);
 7          String number = input.nextLine();
 8          ArrayList<String> list1 = new ArrayList<String>();
 9          ArrayList<String> list2 = new ArrayList<String>();
10          int m = 0;
11          for(int i = 0;i < number.length();i++) {
12              if (number.charAt(i) == ' ') {
13                  m++;
14              }
15              if (number.charAt(i) == ' ') {
16                  list1.add(number.substring(0,i));
17                  list1.add(number.substring(i+1));
18              }
19          }
20          if (m >= 2) {
21              System.out.println("wrong number of points");
22              System.exit(0);
23          }
24         if(!number.matches("^[+-]?(0|[1-9][\\d]*)(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?")) {
25             System.out.println("Wrong Format");
26             System.exit(0);
27         }
28          int num = list1.get(0).indexOf(',');
29          int n = 0;
30          list2.add(list1.get(0).substring(0,num));
31          list2.add(list1.get(0).substring(num + 1));
32          int num1 = list1.get(1).indexOf(',');
33          list2.add(list1.get(1).substring(0,num1));
34          list2.add(list1.get(1).substring(num1 + 1));
35          double x1 = Double.parseDouble(list2.get(0));
36          double y1 = Double.parseDouble(list2.get(1));
37          double x2 = Double.parseDouble(list2.get(2));
38          double y2 = Double.parseDouble(list2.get(3));
39          double length = Math.sqrt(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
40          System.out.println(length);
41     }
42 }

因为不知道要留程序运行测试时结果的截图,所以没有bug的例子,但是出现的bug在写的过程中确实是有一点多,学的还是不够精。

题目3.7.2

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7         String str = in.nextLine();
  8         int i=0;
  9         if(!(str.matches("[1-5][\\:][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?"))&&!(str.matches("[1-5][\\:][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?"))&&!(str.matches("[1-5][\\:][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s]?"))&&!(str.matches("[1-5][\\:][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s]?[\\s][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\,][+-]?(0|[1-9][\\d]{0,})(\\.[\\d]{1,})?[\\s]?")))
 10         {
 11             System.out.print("Wrong Format");
 12         }
 13         else {
 14         Work workin = new Work(i,str);
 15         workin.main();
 16         }
 17     }
 18 
 19 //    [1-5][\\:](([+-]?\\d+)(\\.\\d+)?(\\,)?([+-]?\\d+)(\\.\\d+)?(\\s)?)+
 20 }
 21 class Work {
 22 
 23 
 24     int i;
 25     String str;
 26 
 27     public Work(int i, String str)
 28     {
 29         this.i = i;
 30         this.str = str;
 31     }
 32 
 33     public Work(String str)
 34     {
 35         this.str = str;
 36     }
 37     public void main() {
 38         String str1="";
 39         for(int j=2;j<str.length();j++) {
 40             str1+=str.charAt(j);
 41         }
 42         double []num = split(str1);
 43         switch(str.charAt(0)-'0') {
 44         case 1:
 45             double k;
 46         if(num(str1)==1) {
 47             if(num[0]==num[2])
 48             {
 49                 if(num[1]==num[3])
 50                 {
 51                     System.out.println("points coincide");
 52                 }
 53                 else
 54                 {
 55                     System.out.println("Slope does not exist");
 56                 }
 57             }
 58             else
 59             {
 60                 k = (num[1]-num[3])/(num[0]-num[2]);
 61                 System.out.println(k);
 62             }
 63         }
 64             else
 65                 System.out.print("wrong number of points");
 66             break;
 67         case 2:
 68             if(num(str1)==2) {
 69                 if(num[2]==num[4]&&num[3]==num[5]) {
 70                     System.out.print("points coincide");break;
 71                     }
 72                 if(num[2]==num[4]&&num[3]!=num[5])
 73                 {
 74                     System.out.println(Math.abs(num[0]-num[2]));
 75                 }
 76                 else
 77                 {
 78                     double k1 = (num[3]-num[5])/(num[2]-num[4]);
 79                     double b = num[3]-k1*num[2];
 80                     double d = (k1*num[0]+b-num[1])/(Math.sqrt(1+k1*k1));
 81                     System.out.println(Math.abs(d));
 82                 }
 83             }
 84             else
 85                 System.out.print("wrong number of points");
 86             break;
 87         case 3:
 88             if(num(str1)==2) {
 89                 if(num[0]==num[2]&&num[1]==num[3]||num[4]==num[0]&&num[5]==num[1]||num[4]==num[2]&&num[5]==num[3]) {
 90                     System.out.print("points coincide");break;
 91                     }
 92                 else
 93                 {
 94                     double sum=(num[0]*num[3]-num[2]*num[1])+(num[2]*num[5]-num[4]*num[3])+(num[4]*num[1]-num[5]*num[0]);
 95                     if(sum==0) {
 96                         System.out.print("true");
 97                     }
 98                     else {
 99                         System.out.print("false");
100                     }
101                 }
102             }
103             else
104                 System.out.print("wrong number of points");
105             break;
106         case 4:
107             if(num(str1)==3) {
108                 if(num[0]==num[2]&&num[1]==num[3]||num[4]==num[6]&&num[5]==num[7]) {
109                     System.out.print("points coincide");break;
110                     }
111                 if(num[0]==num[2]&&num[1]!=num[3]||num[4]==num[6]&&num[5]!=num[7])
112                 {
113                     System.out.print("true");
114                 }
115                 else
116                 {
117                     double k1=(num[1]-num[3])/(num[0]-num[2]);
118                     double k2=(num[5]-num[7])/(num[4]-num[6]);
119                     if(Math.abs(k1-k2)<0.00001) {
120                         System.out.print("true");
121                     }
122                     else
123                         System.out.print("false");
124                 }
125             }
126             else
127                 System.out.print("wrong number of points");
128             break;
129         case 5:
130             if(num(str1)==3) {
131                 if(num[0]==num[2]&&num[1]==num[3]||num[4]==num[6]&&num[5]==num[7]) {
132                     System.out.print("points coincide");break;
133                     }
134                 double k1=(num[1]-num[3])/(num[0]-num[2]);
135                 double k2=(num[5]-num[7])/(num[4]-num[6]);
136                 if(Math.abs(k1-k2)<0.00001)
137                 {
138                     System.out.print("is parallel lines,have no intersection point");break;
139                 }
140             }
141             else
142                 System.out.print("wrong number of points");break;
143         }
144     }
145     public static int num(String args) {
146         int count=0;
147         for(int i=0;i<args.length();i++)
148             {
149                 if(args.charAt(i) == ' ')
150                     count++;
151             }
152                 return count;
153     }
154     public double[] split(String str)
155     {
156         String[] Str = str.split(",|\\s|:");
157         double []num = new double[Str.length];
158         for(int i = 0 ;i < Str.length; i++)
159         {
160             num[i] = Double.parseDouble(Str[i]);
161         }
162         return num;
163     }
164 }

ps:因为数学和java输出的知识有些匮乏,所以case5没有写完全,少了如何判断并输出交叉点是否在两条线段之内。

(在此题目中,判断两个浮点数是否相等时应该看两浮点数之差是否足够小)

在此题目测试过程中输出的结果有时会报错,程序的bug在一步步的筛查后在eclipse上也改正了过来,但是没有测试的截图......

题目3.7-3

此题目因为自己不会判断三个点怎么可以构成一个三角形所以提交的源码与题目3.7-2一样,就不展示了。

三、总结
    在这三次的作业和这几周对java的学习,我觉得java和c语言还是有很多的不同,但是二者互通的部分还是很多的,而且自己还有很多没有学到的地方,java的基本的程序设计、选择、循环、集合、字符、字符串、方法以及对象和类的使用再加上一点正则表达式的运用并不是很灵活自如,老师也经常强调分类的重要性,强调从面向过程转变到面向对象的思想转变,对具有较大难度的题目并不能很好的靠自己解决,并且我也意识到再写代码之前应该要做好准备工作,画出类图也是必要的过程,对编写程序也有极大的帮助,最后还要加强对分类的使用,以帮助自己更好的解决难度大的问题。

    在之后的学习生活中我会更加努力的学习面向对象程序设计这门课程,争取取得好的成绩。

 

 

 

 

 

    

         

 

标签:总结,String,get,int,习题集,+-,System,PTA,str
来源: https://www.cnblogs.com/21201628-xujicheng/p/16124519.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有