题目集1~3的总结性Blog
前言:
题目集1:本次题目集所涉及的知识点大部分都是 java 的基本语法,难度较小,分数占比最高的最后一题也仅是逻辑略复杂一些,所以此次的题量较大,总体难度较小。
题目集2:本次题目集所涉及的知识点主要是对字符以及字符串的处理、解析和提取,难度也不大,分数占比最高的第二题所涉及的东西要更多一些,此次的题量小,总体难度较小。
题目集3:本次题目集所涉及的知识点主要是 java 类与对象的设计,以及类的重复使用等,三道题目由浅入深,从点到线再到三角形,难度逐渐增加,这次题目的难度较大,特别是后 两题关于线和三角形的计算较多,所涉及的数学知识也很多,要将数学知识转化为计算机所能识别的代码,这是一个难点,虽然这次题目集只有三道题,但是总的来说难度 大。
设计与分析:
题目集2:
7-2 串口字符解析
RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner in = new Scanner(System.in); 6 String s = new String(); 7 int i=0,num=1,sum=0; 8 char a='2'; 9 s = in.next(); 10 11 if(s.length()<11) { 12 System.out.println("null data"); 13 num++; 14 } 15 else{ 16 for( ; i<s.length(); i++) { 17 if(i+10<s.length()) { 18 if(s.charAt(i) == '0') { 19 for(int n=1; n<9; n++) 20 if(s.charAt(i+n) == '1') 21 sum++; 22 if(sum%2 == 0) 23 a='1'; 24 else 25 a='0'; 26 if(a == s.charAt(i+9) &&s.charAt(i+10) == '1') { 27 System.out.print(num+":"); 28 for(int n=1; n<9; n++) { 29 30 System.out.print(s.charAt(i+n));} 31 System.out.println(); 32 33 num++; 34 } 35 36 else if(s.charAt(i+10) == '1') { 37 System.out.println(num+":parity check error"); 38 39 num++; 40 } 41 else { 42 System.out.println(num+":validate error"); 43 44 num++; 45 } 46 i+=10; 47 } 48 } 49 sum=0; 50 } 51 } 52 if(num == 1) { 53 System.out.println("null data"); 54 } 55 in.close(); 56 } 57 }
本次代码在设计时未使用类与对象的思想,这是一个大的缺点,当然也有学习 Java 这门课程的时间较短的原因,相信随着之后学习的深入,我会逐步改进自己的编程思想;这次代码未使用函数思想并且存在大量的 if-else 语句,这使得这次的代码可读性差,这一点是我在编写时未曾考虑到的,在学习的深入以及一些 java 编程题目练习之后我认识到这是一个很大的问题,当面临一个复杂的问题,代码量到达了几万,几十万,甚至上百万时,这个不良的习惯会导致编写变得非常困难,所以我在之后的题目练习中要逐步养成好的编程习惯;还有一个问题是我在解决这个问题时没有用到一些已有的类来对字符串进行分析,比如 String 类 等,这点以后要加强,避免代码可读性和复杂性过高。
7-3 String的格式判断与内容提取
学校学生学号格式定义如下:
2位年级号+2位学院号+2位班级号+2位序号,如19041103,
编写程序处理用全院学生学号连接起来的长字符串,学院编号为20,包括17、18、19、20四个年级,请从字符串中提取特定两个班级202017班、202061班同学的学号后四位输出,输出编号之间用空格分隔,不换行。
注意:需要排除非法输入。
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner in = new Scanner(System.in); 6 String s = new String(); 7 s = in.nextLine(); 8 boolean a = true; 9 if(s.length()%8 != 0) 10 a=false; 11 else { 12 for(int i=0; i<s.length(); i++) { 13 if(s.charAt(i)<'0'||s.charAt(i)>'9') 14 a=false; 15 } 16 for(int i=0; i<s.length(); i+=8) { 17 if(s.charAt(i+2)!='2'||s.charAt(i+3)!='0') 18 a=false; 19 if(s.charAt(i)!='1') { 20 if(s.charAt(i+1)!='7'&&s.charAt(i+1)!='8'&&s.charAt(i+1)!='9') 21 a=false; 22 } 23 if(s.charAt(i)=='2'&&s.charAt(i+1)=='0') 24 a=true; 25 } 26 } 27 if(a==true) { 28 int num=1; 29 for(int i=0; i<s.length(); i+=8) { 30 if(s.charAt(i)=='2'||s.charAt(i+1)=='0') 31 if((s.charAt(i+4)=='1'&&s.charAt(i+5)=='7')||(s.charAt(i+4)=='6'&&s.charAt(i+5)=='1')) 32 if(num==1) { 33 System.out.printf("%c%c%c%c",s.charAt(i+4),s.charAt(i+5),s.charAt(i+6),s.charAt(i+7)); 34 num++; 35 } 36 else 37 System.out.printf(" %c%c%c%c",s.charAt(i+4),s.charAt(i+5),s.charAt(i+6),s.charAt(i+7)); 38 } 39 } 40 else 41 System.out.print("Wrong Format"); 42 in.close(); 43 } 44 }
这个题目应该将格式判断,内容提取,输出等步骤进行分离,增强代码的可读性;且因为这个题目是对学号字符串进行判断,所以可以使用已有的 String类 里的如 split() 等方法
来对输入字符串进行解析,提取;这个代码需要改进的地方还有很多,我还要学习的东西也还有很多。
题目集3:
7-1 点线形系列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 in = new Scanner(System.in); 7 String s = new String(); 8 s = in.nextLine(); 9 10 Judge judge = new Judge(); 11 judge.Judge(s,2); 12 13 int err = judge.judge(); 14 if(err == 1) { 15 System.out.println("Wrong Format"); 16 } 17 else if(err == 2) { 18 System.out.println("wrong number of points"); 19 } 20 else { 21 Trans c = new Trans(); 22 c.Trans(s); 23 ArrayList<Double> gather = c.trans(); 24 Count count = new Count(); 25 count.Count(gather, 0); 26 double result = count.count(); 27 System.out.println(result); 28 } 29 30 in.close(); 31 } 32 } 33 34 class Judge{ 35 String s = new String(); 36 String standard = "^(-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*[1-9])?[,](-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*[1-9])?$"; 37 int n;//应有的坐标数 38 39 void Judge(String s, int n) { 40 this.s=s; 41 this.n=n; 42 } 43 44 int judge() { 45 int p = 0; 46 String[] str =s.split(" "); 47 48 if(judge1(str)!=0) { 49 p=1; 50 } 51 52 else if(judge2(str)!=0) { 53 p=2; 54 } 55 return p; 56 } 57 58 int judge1(String[] str) { 59 int p = 0; 60 for(int i=0; i<str.length; i++) { 61 if(!str[i].matches(standard)) 62 p=1; 63 } 64 return p; 65 } 66 67 int getStringCount(String str, char key) { 68 int count = 0; 69 int index = str.indexOf(key); 70 while (index != -1) { 71 count++; 72 str = str.substring(index + 1); 73 index = str.indexOf(key); 74 } 75 return count; 76 } 77 78 int judge2(String[] str) { 79 int p = 0; 80 if(str.length != n) 81 p=2; 82 return p; 83 } 84 } 85 86 class Trans{ 87 ArrayList<Double> gather = new ArrayList<>(); 88 Cpoint a = new Cpoint(); 89 String s; 90 91 void Trans(String s) { 92 this.s=s; 93 } 94 //输入:字符串 95 //操作:转换为double并保存至链表gather 96 //输出:链表gather 97 ArrayList<Double> trans() { 98 int num = getStringCount(s, ','); 99 String[] str =s.split(" "); 100 101 for(int i=0; i<num; i++) { 102 a.tran(str[i]); 103 gather.add(a.x); 104 gather.add(a.y); 105 } 106 107 return gather; 108 } 109 110 int getStringCount(String str, char key) { 111 int count = 0; 112 int index = str.indexOf(key); 113 while (index != -1) { 114 count++; 115 str = str.substring(index + 1); 116 index = str.indexOf(key); 117 } 118 return count; 119 } 120 } 121 122 class Cpoint{ 123 double x; 124 double y; 125 //输入:(x,y) 126 //操作:转换为double 127 //输出:x和y的double形式 128 void tran(String s) { 129 String[] str =s.split(","); 130 131 x=transdouble(str[0]); 132 133 y=transdouble(str[1]); 134 } 135 136 double transdouble(String s) { 137 double num = 0; 138 int index = s.indexOf('.'); 139 int overindex=-1; 140 if(index>0) { 141 for(int i=index-1; i>-1; i--) { 142 if(s.charAt(i)<'0'||s.charAt(i)>'9') { 143 overindex=i; 144 break; 145 } 146 num+=(s.charAt(i)-'0')*Math.pow(10,index-i-1); 147 } 148 149 for(int i=index+1; i<s.length(); i++) { 150 num+=(s.charAt(i)-'0')*Math.pow(10,index-i); 151 } 152 } 153 else { 154 for(int i=s.length()-1; i>-1; i--) { 155 if(s.charAt(i)<'0'||s.charAt(i)>'9') { 156 overindex=i; 157 break; 158 } 159 num+=(s.charAt(i)-'0')*Math.pow(10,s.length()-1-i); 160 } 161 } 162 163 if(overindex!=-1) { 164 if(s.charAt(overindex) == '-') 165 num=-num; 166 } 167 return num; 168 } 169 } 170 171 class Count{ 172 ArrayList<Double> gather = new ArrayList<>(); 173 int operation; 174 175 void Count(ArrayList<Double> gather,int operation){ 176 this.gather=gather; 177 this.operation=operation; 178 } 179 180 double count() { 181 double d=-1; 182 switch(operation) { 183 case 0: d = Math.sqrt((gather.get(2)-gather.get(0))*(gather.get(2)-gather.get(0))+(gather.get(3)-gather.get(1))*(gather.get(3)-gather.get(1))); 184 } 185 186 return d; 187 } 188 }
很显然,我将这个题目复杂化了,这是一个很差劲的代码。这次我初步运用了类与对象的概念,分别做了 判断 转换 点 三个类,这三个类所做的事情可以写在 点 这一个类里完成,且后两题完全可以以 点 作为最基本参数来进行操作,这让我认识到提前设计 类图 是一件多么重要的事情,能为我们理清思路,减少很多的坑。
7-2 点线形系列2-线的计算
用户输入一组选项和数据,进行与直线有关的计算。选项包括:
1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。
2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。
3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false。
4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.
5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"。
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。
例如:1:0,0 1,1
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出"points coincide",
1 import java.util.Scanner; 2 import java.util.ArrayList; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 String s = new String(); 8 s = in.nextLine(); 9 10 Judge judge = new Judge(); 11 switch (s.charAt(0)) { 12 case '1':; 13 case '2':; 14 case '3':{judge.Judge(s.substring(2),3);break;} 15 case '4':{judge.Judge(s.substring(2),5);break;} 16 case '5':{judge.Judge(s.substring(2),4);break;} 17 } 18 19 int err = judge.judge(); 20 if(s.charAt(1)!=':') err=1; 21 if(err == 1) { 22 System.out.println("Wrong Format"); 23 } 24 else if(err == 2) { 25 System.out.println("wrong number of points"); 26 } 27 else { 28 29 Trans c = new Trans(); 30 c.Trans(s.substring(2)); 31 double[][] gather = c.trans(); 32 33 Point a = new Point(); 34 a.Point(gather[gather.length-3][0],gather[gather.length-3][1]); 35 Point b = new Point(); 36 b.Point(gather[gather.length-2][0],gather[gather.length-2][1]); 37 Point d = new Point(); 38 d.Point(gather[gather.length-1][0],gather[gather.length-1][1]); 39 Triangle triangle = new Triangle(); 40 boolean m=triangle.Triangle(a, b, d); 41 Line line= new Line(); 42 boolean n=false; 43 if(s.charAt(0)=='4') { 44 Point e = new Point(); 45 e.Point(gather[0][0],gather[0][1]); 46 Point f = new Point(); 47 f.Point(gather[1][0],gather[1][1]); 48 n=line.Line(e, f); 49 if(n) 50 System.out.println("points coincide"); 51 } 52 if((line.Line(a, b)||line.Line(a, d)||line.Line(b, d)||!m)&&!n) 53 System.out.println("data error"); 54 else if(!n) 55 count(s.charAt(0),gather,triangle); 56 57 } 58 59 in.close(); 60 } 61 62 static void count(char operation,double[][] gather,Triangle triangle) { 63 switch(operation) { 64 case '1':{ 65 System.out.println(triangle.dengyao()+" "+triangle.dengbian()); 66 break;} 67 case '2':{ 68 //System.out.printf("%f %f %f,%f"); 69 System.out.println((float)(triangle.zhouchang()+0.0000005)+" "+(float)(triangle.mianji())+" "+(float)(triangle.zhongxin().x)+","); 70 System.out.print((float)(triangle.zhongxin().y+0.0000005)); 71 break;} 72 case '3':{ 73 System.out.println((triangle.jiao()=='d')+" "+(triangle.jiao()=='z')+" "+(triangle.jiao()=='r')); 74 break;} 75 case '4':{ 76 Point a = new Point(); 77 a.Point(gather[0][0],gather[0][1]); 78 Point b = new Point(); 79 b.Point(gather[1][0],gather[1][1]); 80 Line n = new Line(); 81 boolean m=n.Line(a,b); 82 if(!m) 83 triangle.jiaodianshu(n); 84 else 85 System.out.println("points coincide"); 86 break;} 87 case '5':{ 88 89 break;} 90 } 91 } 92 93 } 94 95 class Judge{ 96 String s = new String(); 97 String standard = "^(-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*)?[,](-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*)?$"; 98 int n;//应有的坐标数 99 100 void Judge(String s, int n) { 101 this.s=s; 102 this.n=n; 103 } 104 105 int judge() { 106 int p = 0; 107 String[] str =s.split(" "); 108 109 if(judge1(str)!=0) { 110 p=1; 111 } 112 113 else if(judge2(str)!=0) { 114 p=2; 115 } 116 117 return p; 118 } 119 120 int judge1(String[] str) { 121 int p = 0; 122 for(int i=0; i<str.length; i++) { 123 if(!str[i].matches(standard)) { 124 p=1; 125 break; 126 } 127 } 128 return p; 129 } 130 131 int judge2(String[] str) { 132 int p = 0; 133 if(str.length != n) 134 p=2; 135 return p; 136 } 137 138 } 139 140 class Trans{ 141 ArrayList<Double> gather = new ArrayList<>(); 142 String s; 143 double x; 144 double y; 145 int n; 146 147 void Trans(String s) { 148 this.s=s; 149 } 150 //输入:字符串 151 //操作:转换为double并保存至链表gather 152 //输出:链表gather 153 double[][] trans() { 154 String[] str =s.split(" "); 155 156 for(int i=0; i<str.length; i++) { 157 tran(str[i]); 158 gather.add(x); 159 gather.add(y); 160 } 161 162 n=gather.size()/2; 163 double[][] D = new double[n][2]; 164 for(int i=0; i<n; i++) { 165 D[i][0]=gather.get(2*i); 166 D[i][1]=gather.get(2*i+1); 167 } 168 return D; 169 } 170 171 void tran(String s) { 172 String[] str =s.split(","); 173 174 x=transdouble(str[0]); 175 176 y=transdouble(str[1]); 177 } 178 179 double transdouble(String s) { 180 double num = Double.parseDouble(s); 181 182 return num; 183 } 184 } 185 186 class Point{ 187 double x; 188 double y; 189 190 void Point(double x,double y) { 191 this.x=x; 192 this.y=y; 193 } 194 195 double juli(Point a) { 196 return Math.sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y)); 197 } 198 199 } 200 201 class Line{ 202 Point a; 203 Point b; 204 double A; 205 double B; 206 double C; 207 208 boolean Line(Point a, Point b) { 209 this.a=a; 210 this.b=b; 211 A=b.y-a.y; 212 B=a.x-b.x; 213 C=(b.x*a.y-a.x*b.y); 214 return (a.x==b.x&&a.y==b.y); 215 } 216 217 boolean chuizhi() { 218 return (a.x==b.x); 219 } 220 221 double xielv() { 222 if(((a.y-b.y)/(a.x-b.x))==-0.0) 223 return 0; 224 return ((a.y-b.y)/(a.x-b.x)); 225 } 226 227 double juli(Point c) { 228 if(a.x==b.x) { 229 return Math.abs(c.x-a.x); 230 } 231 return Math.abs(A*c.x+B*c.y+C)/Math.sqrt(A*A+B*B); 232 } 233 234 boolean pingxing(Line c) { 235 boolean px; 236 Line ab = new Line(); 237 ab.Line(a, b); 238 if((ab.chuizhi()&&c.chuizhi())||(ab.A==c.A&&ab.B==c.B&&ab.C==c.C)) 239 px=true; 240 else 241 px = (ab.xielv()==c.xielv()); 242 return px; 243 } 244 245 Point jiaodian(Line c) { 246 Point e = new Point(); 247 e.x=(B*c.C-c.B*C)/(A*c.B-c.A*B); 248 e.y=-(A*c.C-c.A*C)/(A*c.B-c.A*B); 249 return e; 250 } 251 252 boolean nei(Point e) { 253 boolean n=false; 254 if((e.y>a.y&&e.y<b.y)||(e.y<a.y&&e.y>b.y)) 255 n=true; 256 257 return n; 258 } 259 }
这个代码是在听了老师的课之后,明白了 面对对象编程 与面对过程编程 之间的一些区别,并且跟随老师学习了解决这三道题的设计 类 的基本思路之后写的。由于最初写第一题时就设计了 判断 转换 类,所以未再进行删减更改。相对于第一题,这里使用了正则表达式,并且对各类里的一些函数进行了改进,删减了一些不必要的内容,改善了一些逻辑方面的代码等。增加的 线 的类,在写 线这个类的时候我对类与类之间的关系有了一些了解,比如 线 的基本参数可以是两个点组成; 线 这个类的一些方法之间可以相互调用,避免重复编写,增加代码量,降低可读性。
7-3 点线形系列3-三角形的计算
用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。
2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,
4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"
5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
如果输入的三个点无法构成三角形,输出"data error"。
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0
选项4中所输入线的两个点坐标重合,输出"points coincide",
1 import java.util.Scanner; 2 import java.util.ArrayList; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 String s = new String(); 8 s = in.nextLine(); 9 10 Judge judge = new Judge(); 11 switch (s.charAt(0)) { 12 case '1':; 13 case '2':; 14 case '3':{judge.Judge(s.substring(2),3);break;} 15 case '4':{judge.Judge(s.substring(2),5);break;} 16 case '5':{judge.Judge(s.substring(2),4);break;} 17 } 18 19 int err = judge.judge(); 20 if(s.charAt(1)!=':') err=1; 21 if(err == 1) { 22 System.out.println("Wrong Format"); 23 } 24 else if(err == 2) { 25 System.out.println("wrong number of points"); 26 } 27 else { 28 29 Trans c = new Trans(); 30 c.Trans(s.substring(2)); 31 double[][] gather = c.trans(); 32 33 Point a = new Point(); 34 a.Point(gather[gather.length-3][0],gather[gather.length-3][1]); 35 Point b = new Point(); 36 b.Point(gather[gather.length-2][0],gather[gather.length-2][1]); 37 Point d = new Point(); 38 d.Point(gather[gather.length-1][0],gather[gather.length-1][1]); 39 Triangle triangle = new Triangle(); 40 boolean m=triangle.Triangle(a, b, d); 41 Line line= new Line(); 42 boolean n=false; 43 if(s.charAt(0)=='4') { 44 Point e = new Point(); 45 e.Point(gather[0][0],gather[0][1]); 46 Point f = new Point(); 47 f.Point(gather[1][0],gather[1][1]); 48 n=line.Line(e, f); 49 if(n) 50 System.out.println("points coincide"); 51 } 52 if((line.Line(a, b)||line.Line(a, d)||line.Line(b, d)||!m)&&!n) 53 System.out.println("data error"); 54 else if(!n) 55 count(s.charAt(0),gather,triangle); 56 57 } 58 59 in.close(); 60 } 61 62 static void count(char operation,double[][] gather,Triangle triangle) { 63 switch(operation) { 64 case '1':{ 65 System.out.println(triangle.dengyao()+" "+triangle.dengbian()); 66 break;} 67 case '2':{ 68 out(triangle.zhouchang()); 69 System.out.print(" "); 70 out(triangle.mianji()); 71 System.out.print(" "); 72 out(triangle.zhongxin().x); 73 System.out.print(","); 74 out(triangle.zhongxin().y); 75 76 break;} 77 case '3':{ 78 System.out.println((triangle.jiao()=='d')+" "+(triangle.jiao()=='z')+" "+(triangle.jiao()=='r')); 79 break;} 80 case '4':{ 81 Point a = new Point(); 82 a.Point(gather[0][0],gather[0][1]); 83 Point b = new Point(); 84 b.Point(gather[1][0],gather[1][1]); 85 Line n = new Line(); 86 boolean m=n.Line(a,b); 87 if(!m) { 88 if(triangle.jiaodianshu(n)!=-1) 89 System.out.println(triangle.jiaodianshu(n)); 90 else 91 System.out.println("The point is on the edge of the triangle"); 92 } 93 else 94 System.out.println("points coincide"); 95 break;} 96 case '5':{ 97 Point a = new Point(); 98 a.Point(gather[0][0],gather[0][1]); 99 Line y = new Line(); 100 101 if(triangle.neibu(a)) { 102 boolean o=true; 103 y.Line(triangle.a, triangle.b); 104 if(Math.abs(y.juli(a)-0)<0.000000001&&y.nei(a)) { 105 System.out.println("on the triangle"); 106 o=false; 107 } 108 y.Line(triangle.a, triangle.c); 109 if(Math.abs(y.juli(a)-0)<0.000000001&&o&&y.nei(a)) { 110 System.out.println("on the triangle"); 111 o=false; 112 } 113 y.Line(triangle.b, triangle.c); 114 if(Math.abs(y.juli(a)-0)<0.000000001&&o&&y.nei(a)) { 115 System.out.println("on the triangle"); 116 o=false; 117 } 118 if(o) 119 System.out.println("in the triangle"); 120 } 121 else 122 System.out.println("outof the triangle"); 123 break;} 124 } 125 } 126 127 static void out(double i) { 128 // int d = (int)(i/0.0000001)-(int)(i/0.000001)*10; 129 int e= (int)(i/0.000001)-(int)(i/0.00001)*10; 130 if(e==0) 131 System.out.print((float)i); 132 else 133 System.out.printf("%.6f",(float)i); 134 } 135 } 136 137 class Judge{ 138 String s = new String(); 139 String standard = "^(-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*)?[,](-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*)?$"; 140 int n;//应有的坐标数 141 142 void Judge(String s, int n) { 143 this.s=s; 144 this.n=n; 145 } 146 147 int judge() { 148 int p = 0; 149 String[] str =s.split(" "); 150 151 if(judge1(str)!=0) { 152 p=1; 153 } 154 155 else if(judge2(str)!=0) { 156 p=2; 157 } 158 159 return p; 160 } 161 162 int judge1(String[] str) { 163 int p = 0; 164 for(int i=0; i<str.length; i++) { 165 if(!str[i].matches(standard)) { 166 p=1; 167 break; 168 } 169 } 170 return p; 171 } 172 173 int judge2(String[] str) { 174 int p = 0; 175 if(str.length != n) 176 p=2; 177 return p; 178 } 179 180 } 181 182 class Trans{ 183 ArrayList<Double> gather = new ArrayList<>(); 184 String s; 185 double x; 186 double y; 187 int n; 188 189 void Trans(String s) { 190 this.s=s; 191 } 192 //输入:字符串 193 //操作:转换为double并保存至链表gather 194 //输出:链表gather 195 double[][] trans() { 196 String[] str =s.split(" "); 197 198 for(int i=0; i<str.length; i++) { 199 tran(str[i]); 200 gather.add(x); 201 gather.add(y); 202 } 203 204 n=gather.size()/2; 205 double[][] D = new double[n][2]; 206 for(int i=0; i<n; i++) { 207 D[i][0]=gather.get(2*i); 208 D[i][1]=gather.get(2*i+1); 209 } 210 return D; 211 } 212 213 void tran(String s) { 214 String[] str =s.split(","); 215 216 x=transdouble(str[0]); 217 218 y=transdouble(str[1]); 219 } 220 221 double transdouble(String s) { 222 double num = Double.parseDouble(s); 223 224 return num; 225 } 226 } 227 228 class Point{ 229 double x; 230 double y; 231 232 void Point(double x,double y) { 233 this.x=x; 234 this.y=y; 235 } 236 237 double juli(Point a) { 238 return Math.sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y)); 239 } 240 241 } 242 243 class Line{ 244 Point a; 245 Point b; 246 double A; 247 double B; 248 double C; 249 250 boolean Line(Point a, Point b) { 251 this.a=a; 252 this.b=b; 253 A=b.y-a.y; 254 B=a.x-b.x; 255 C=(b.x*a.y-a.x*b.y); 256 return (a.x==b.x&&a.y==b.y); 257 } 258 259 boolean chuizhi() { 260 return (a.x==b.x); 261 } 262 263 double xielv() { 264 if(((a.y-b.y)/(a.x-b.x))==-0.0) 265 return 0; 266 return ((a.y-b.y)/(a.x-b.x)); 267 } 268 269 double juli(Point c) { 270 if(a.x==b.x) { 271 return Math.abs(c.x-a.x); 272 } 273 return Math.abs(A*c.x+B*c.y+C)/Math.sqrt(A*A+B*B); 274 } 275 276 boolean pingxing(Line c) { 277 boolean px; 278 Line ab = new Line(); 279 ab.Line(a, b); 280 if((ab.chuizhi()&&c.chuizhi())||(ab.A==c.A&&ab.B==c.B&&ab.C==c.C)) 281 px=true; 282 else 283 px = (ab.xielv()==c.xielv()); 284 return px; 285 } 286 287 Point jiaodian(Line c) { 288 Point e = new Point(); 289 e.x=(B*c.C-c.B*C)/(A*c.B-c.A*B); 290 e.y=-(A*c.C-c.A*C)/(A*c.B-c.A*B); 291 return e; 292 } 293 294 boolean nei(Point e) { 295 boolean n=false; 296 if((e.y>a.y&&e.y<b.y)||(e.y<a.y&&e.y>b.y)) 297 n=true; 298 if((e.x>a.x&&e.x<b.x)||(e.x<a.x&&e.x>b.x)) 299 n=true; 300 return n; 301 } 302 } 303 304 class Triangle{ 305 Point a; 306 Point b; 307 Point c; 308 309 boolean Triangle(Point a,Point b,Point c) { 310 this.a=a; 311 this.b=b; 312 this.c=c; 313 boolean m = true; 314 if(a.juli(b)>=(a.juli(c)+b.juli(c))) 315 m=false; 316 else if(a.juli(c)>=(a.juli(b)+b.juli(c))) 317 m=false; 318 else if(b.juli(c)>=(a.juli(b)+a.juli(c))) 319 m=false; 320 return m; 321 } 322 323 boolean dengyao() { 324 if((a.juli(b)==a.juli(c))||(a.juli(b)==b.juli(c))||(a.juli(c)==b.juli(c))) 325 return true; 326 else 327 return false; 328 } 329 330 boolean dengbian() { 331 if(a.juli(b)==a.juli(c)&&a.juli(b)==b.juli(c)) 332 return true; 333 else 334 return false; 335 } 336 337 double zhouchang() { 338 return (a.juli(b)+a.juli(c)+b.juli(c)); 339 } 340 341 double mianji() { 342 double p=zhouchang()/2; 343 return (Math.sqrt(p*(p-a.juli(b))*(p-a.juli(c))*(p-b.juli(c)))); 344 } 345 346 Point zhongxin() { 347 Point m = new Point(); 348 m.x=(a.x+b.x+c.x)/3.0; 349 m.y=(a.y+b.y+c.y)/3.0; 350 return m; 351 } 352 353 char jiao() { 354 char m='r'; 355 356 if(a.juli(b)*a.juli(b)+b.juli(c)*b.juli(c)<a.juli(c)*a.juli(c)) 357 m='d'; 358 else if(a.juli(c)*a.juli(c)+b.juli(c)*b.juli(c)<a.juli(b)*a.juli(b)) 359 m='d'; 360 else if(a.juli(b)*a.juli(b)+a.juli(c)*a.juli(c)<b.juli(c)*b.juli(c)) 361 m='d'; 362 if(Math.abs(a.juli(b)*a.juli(b)+b.juli(c)*b.juli(c)-a.juli(c)*a.juli(c))<0.000000001) 363 m='z'; 364 else if(Math.abs(a.juli(c)*a.juli(c)+b.juli(c)*b.juli(c))-a.juli(b)*a.juli(b)<0.000000001) 365 m='z'; 366 else if(Math.abs(a.juli(b)*a.juli(b)+a.juli(c)*a.juli(c)-b.juli(c)*b.juli(c))<0.000000001) 367 m='z'; 368 369 return m; 370 } 371 372 int jiaodianshu(Line n) { 373 int m=0; 374 Line q = new Line(); 375 if(!q.Line(a, b)) { 376 if(n.juli(a)==0&&n.juli(b)==0) 377 return -1; 378 if(q.nei(q.jiaodian(n))) 379 m+=1; 380 if(n.juli(a)==0) 381 m+=1; 382 } 383 if(!q.Line(a, c)) { 384 if(n.juli(a)==0&&n.juli(c)==0) 385 return -1; 386 if(q.nei(q.jiaodian(n))) 387 m+=1; 388 if(n.juli(b)==0) 389 m+=1; 390 } 391 if(!q.Line(b, c)) { 392 if(n.juli(c)==0&&n.juli(b)==0) 393 return -1; 394 if(q.nei(q.jiaodian(n))) 395 m+=1; 396 if(n.juli(c)==0) 397 m+=1; 398 } 399 return m; 400 } 401 402 boolean neibu(Point g) { 403 boolean h = false; 404 double sum=0; 405 Line q = new Line(); 406 q.Line(a, b); 407 sum+=a.juli(b)*q.juli(g)/2; 408 q.Line(a, c); 409 sum+=a.juli(c)*q.juli(g)/2; 410 q.Line(b, c); 411 sum+=b.juli(c)*q.juli(g)/2; 412 if(Math.abs(mianji()-sum)<0.0000000001) 413 h=true; 414 return h; 415 } 416 417 }
这题直至截止时间我也没有拿到满分,尤其是对于输出格式问题一直没有想到一个合适的方法来解决;选项4非输出格式错误的几个测试点和选项5的几个测试点也没有通过,很遗憾,我没能在规定的时间内解决这道题。从这道题里我也发现类与类之间有很多的关联,比如在这道题里就有不少的地方用到了 点 和 线 这两个类里的方法,求距离,求交点,这些都可以使用到已有的类方法。后两题使用到的数学方法较多,如何把我们学过的知识写成代码这是一件很有挑战性的事情,用等式来解决复杂的数学问题,将问题用逻辑一步一步的串联起来,这是解决这道题所需要的最基本的东西。在这道题上我也更加认识到了自身的不足,这个代码也还存在很多的问题。逻辑问题,书写问题,时间空间复杂度问题,这一个个的问题都要之后通过不断地学习,不断地练习来解决它们,我也相信我将来能够做到。
采坑心得:
源码在提交过程中遇到过很多的问题,这里不一一赘述,其中我遇到的最大的问题反而是最小的问题,如同
1、题目集3 里的第二题的正则表达式
改前:
String standard = "^(-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*[1-9])?[,](-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*[1-9])?$";
改后:
String standard = "^(-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*)?[,](-?\\+?(0|[1-9][0-9]*))(\\.[0-9]*)?$";
仅仅是小数点后能否零结尾导致我每一个选项都有1或2个点过不了,这个错误是极其不正常的,开始我认为是其他错误导致浪费了很多的时间,之后对正则表达式进行测试才发现了这个问题所在;在其他题中也出现过类似的问题,每一次都会花费大量的时间来解决它;所以我在以后的代码编写中要小心这种不易查找的”小问题“出现,尽可能做到细致。
2、还有在进行计算时使用的ArrayList链表,开始使用时总是有很多的不方便,特别是在获取数据进行计算时很不方便,之后添加
n=gather.size()/2; double[][] D = new double[n][2]; for(int i=0; i<n; i++) { D[i][0]=gather.get(2*i); D[i][1]=gather.get(2*i+1); } return D; }
将其转换为 double数组 来传参,在使用时就会方便许多,当然也是由于我能力还不够,运用ArrayList链表不熟练导致,但这确实让我之后思路更通畅了些。
3、最后,我在进行一些等值运算时发现浮点数与整形数的一些区别,在进行上网搜索之后我意识到
a.juli(b)*a.juli(b)+b.juli(c)*b.juli(c)==a.juli(c)*a.juli(c))
与
Math.abs(a.juli(b)*a.juli(b)+b.juli(c)*b.juli(c)-a.juli(c)*a.juli(c))<0.000000001
是不一样的,由于浮点数的特殊性,它的精度与我们日常认知存在出入,所以一般在进行浮点数运算时代码都会采用第二种比较方法,相减的绝对值小于一个极小数则认为它们相等,这一点在进行浮点数运算中极为重要,特别是需要使用到 相等 关系时更加要注意。
以上仅仅是我所遇到问题的一部分,但是却让我遇到了较大的坑,这是我这几次踩坑的一点点心得,我自身所存在的问题远不止这些,希望在之后不断地”踩坑“中能不断发现问题,不断解决问题,争取提升自我。
改进建议:
题目集2的7-2、7-3可以用函数形式来表达;题目集3的7-1的类的设计不合理,可将格式检查、字符串转换、点这三个类合并,让代码更简洁明了;题目集3的7-2、7-3两题在设计类方法时传参时不妥,导致方法的重复使用增加了难度,因此我认为在设计方法时设计好传参对于函数重复使用是很重要的。然后就是编写代码的习惯并不好,这要在之后的练习中不断地改进,不断养成一个好的习惯,让代码逻辑性、可读性更佳。
总结:
对于这三次题目集,我学到了 java 基本语法的使用,能够编写 java 程序并运行;学习了对字符及字符串的解析、提取等操作;并且对类与对象有了基本的概念,对面对对象编程也有了基本概念;此外对java与C语言不同的语句也有了更深的理解,处理如题目集3这种较为复杂的问题也学习了更好的思路;总的来说,对于刚接触java的我来说,这三次题目集让我对java有了一些最基本的了解,真正的开始学习java。我还需要改进的地方有很多,代码,逻辑,处理问题的方式等等,其中处理问题的方式是我在之后很长一段时间需要学习并掌握的;然后代码的规范性、变量和方法的命名合理性等等都要不断练习;在之后的练习中我可以通过类图等方式来理清基本逻辑再进行代码的编写,这是改进代码逻辑性的很好方式,我之后编写前要尽量画出类图,尤其是面对难题时更加有必要。对教师、课程、作业、实验、课上及课下组织方式等方面我没有什么意见和建议,有两点建议:1.上课之前希望老师能提前通知,因为有两次课都是突然通知导致我没能够及时进入课堂学习;2.上课时可以多讲一些具体的题目分析,比如pta发布的作业等,因为我个人感觉这对于学习新知识和巩固学过的知识是一个很好的方式,对于我们理解知识点也有一些帮助。

浙公网安备 33010602011771号