(1)前言:前几个题目相对来说难度不算大,代码量也较小,考察了一些比较基础的java基本知识,但是有些地方要注意仔细。三角形计算难度较大,代码量也比较大。
(2)设计与分析:
1.题目集2 7-2 串口字符解析
RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位 数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8 位,奇偶校验位采用奇校验。
输入格式:
由0、1组成的二进制数据流。例如:11110111010111111001001101111111011111111101111
输出格式:
过滤掉空闲、起始、结束以及奇偶校验位之后的数据,数据之前加上序号和英文冒号。
如有多个数据,每个数据单独一行显示。
若数据不足11位或者输入数据全1没有起始位,则输出"null data",
若某个数据的结束符不为1,则输出“validate error”。
若某个数据奇偶校验错误,则输出“parity check error”。
若数据结束符和奇偶校验均不合格,输出“validate error”。
如:11011或11111111111111111。
例如:
1:11101011
2:01001101
3:validate error
源码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String a = in.next(); if(a.length()<11) { System.out.print("null data"); return; } //正则表达式判断输入合法性 if(a.matches("^[1]*$")) { System.out.print("null data"); return; } int start = 0,i=0,num = 1,sum = 0; boolean parity = false,validate = false; for(start = 0;start<a.length()-10;start++) { if(a.charAt(start)=='0') {//开始位置 System.out.print(num+":"); num++; if(a.charAt(start+10)=='0') { validate = false; }else { validate = true; sum = 0; for(i=start+1;i<start+9;i++) {//奇偶位判断(奇判断) if(a.charAt(i)=='1') { sum++; } } if(sum%2==0) { if(a.charAt(start+9)=='1') { parity = true; }else { parity = false; } }else { if(a.charAt(start+9)=='0') { parity = true; }else { parity = false; } } } if(validate == true) { if(parity == true) { for(i=start+1;i<start+9;i++) { System.out.print(a.charAt(i)); } System.out.print("\n"); }else { System.out.println("parity check error"); } }else { System.out.println("validate error"); } start = start + 10; } } } }View Code
2.题目集3 7-1点线形系列1-计算两点之间的距离
输入连个点的坐标,计算两点之间的距离
输入格式:
4个double类型的实数,两个点的x,y坐标,依次是x1、y1、x2、y2,两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。例如:0,0 1,1或0.1,-0.3 +3.5,15.6。
若输入格式非法,输出"Wrong Format"。
若输入格式合法但坐标点的数量超过两个,输出“wrong number of points”。
输出格式:
计算所得的两点之间的距离。例如:1.4142135623730951
考察正则表达式的使用,判断两点的数据是否合法以及点的数量是否合格,不合格输出"Wrong Format“,坐标点的数量超过两个输出“wrong number of points”
源码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); String point[] = input.split(" "); String num[] = null; int cot = 0; for(String i:point) { num = i.split(","); for(String j:num) { //正则表达式 if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) { System.out.print("Wrong Format"); System.exit(0); } } } if(point.length!=2) { System.out.print("wrong number of points"); System.exit(0); } //把输入的转成数字 num = point[0].split(","); double a = Double.valueOf(num[0]); double b = Double.valueOf(num[1]); num = point[1].split(","); double c = Double.valueOf(num[0]); double d = Double.valueOf(num[1]); if(a==c&&b==d){ System.out.print("Wrong Format"); return; } System.out.print(Math.sqrt((a-c)*(a-c)+(b-d)*(b-d))); in.close(); } }View Code
3.题目集3 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",
输出格式:
见题目描述。
源码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str = input.nextLine(); char[] a = new char[100]; a = str.toCharArray(); if (a[1] == ':') { String[] str1 = str.split(":"); String[] str2 = str1[1].split(" "); int i; boolean k = true; int len = str2.length; double x1,y1,x2,y2,x3,y3,x4,y4; for (i = 0;i < len;i++) { k = func(str2[i]); if(k == false) { break; } } switch (a[0]) { case '1':if(len == 2) { String[] s0 = str2[0].split(",");//x1,y1 String[] s1 = str2[1].split(",");//x2,y2 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); xielv(x1, y1, x2, y2); } else { System.out.println("wrong number of points"); } break; case '2':if(len == 3) { String[] s0 = str2[0].split(",");//x1,y1 String[] s1 = str2[1].split(",");//x2,y2 String[] s2 = str2[2].split(",");//x3,y3 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); x3 = Double.valueOf(s2[0]);y3 = Double.valueOf(s2[1]); juli(x1, y1, x2, y2, x3, y3); } else { System.out.println("wrong number of points"); } break; case '3':if(len == 3) { String[] s0 = str2[0].split(",");//x1,y1 String[] s1 = str2[1].split(",");//x2,y2 String[] s2 = str2[2].split(",");//x3,y3 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); x3 = Double.valueOf(s2[0]);y3 = Double.valueOf(s2[1]); samezhixian(x1, y1, x2, y2, x3, y3); } else { System.out.println("wrong number of points"); } break; case '4':if(len == 4) { String[] s0 = str2[0].split(",");//x1,y1 String[] s1 = str2[1].split(",");//x2,y2 String[] s2 = str2[2].split(",");//x3,y3 String[] s3 = str2[3].split(",");//x4,y4 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); x3 = Double.valueOf(s2[0]);y3 = Double.valueOf(s2[1]); x4 = Double.valueOf(s3[0]);y4 = Double.valueOf(s3[1]); pinxing(x1, y1, x2, y2, x3, y3, x4, y4); } else { System.out.println("wrong number of points"); } break; case '5':if(len == 4) { String[] s0 = str2[0].split(",");//x1,y1 String[] s1 = str2[1].split(",");//x2,y2 String[] s2 = str2[2].split(",");//x3,y3 String[] s3 = str2[3].split(",");//x4,y4 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); x3 = Double.valueOf(s2[0]);y3 = Double.valueOf(s2[1]); x4 = Double.valueOf(s3[0]);y4 = Double.valueOf(s3[1]); jiaodian(x1, y1, x2, y2, x3, y3, x4, y4); } else { System.out.println("wrong number of points"); } break; default:System.out.println("Wrong Format");break; } } else { System.out.println("Wrong Format"); } } public static boolean func(String d) //格式是否正确 { String[] a1 = d.split(","); boolean flag =true; String regex = "^([+-]?\\d+)(\\.\\d+)?"; int i; for (i = 0;i < a1.length;i++) { flag = a1[i].matches(regex); if (flag == false) { break; } } if (flag == false) { return false; } else { return true; } } public static boolean chonghe(double x1,double y1,double x2,double y2)//是否重合 { if (x1 == x2 && y1 == y2) { return true; } else { return false; } } public static double xielv(double x1,double y1,double x2,double y2)//斜率,包括垂直 { double k=0; if (chonghe(x1, y1, x2, y2) == false) { if (x1 == x2 ) { System.out.println("Slope does not exist"); } else { k = (y1-y2)/(x1-x2); System.out.println(k); } } else { System.out.println("points coincide"); } return k; } public static double getDistance(double x1,double y1,double x2,double y2) //两点距离 { double r = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return r; } public static void juli(double x1,double y1,double x2,double y2,double x3,double y3)//第一点到另外两点直线距离 { double len = getDistance(x2, y2, x3, y3); double len1 = (x1*y2-x1*y3+x2*y3-x2*y1+x3*y1-x2*y2)/len; if (chonghe(x2, y2, x3, y3) == false) { System.out.println(len1); } else { System.out.println("points coincide"); } } public static void samezhixian(double x1,double y1,double x2,double y2,double x3,double y3)//是否同一条直线 { boolean k = false; double k1=0,k2=0; if (chonghe(x1, y1, x2, y2) == false && chonghe(x1, y1, x3, y3) == false && chonghe(x2, y2, x3, y3) == false) { if(x1 == x2 && x2 == x3) { k = true; System.out.println(k); } else { k1 = (y1-y2)/(x1-x2); k2 = (y2-y3)/(x2-x3); if (k1==k2) { k = true; System.out.println(k); } else { System.out.println(k); } } } else { System.out.println("points coincide"); } } public static void pinxing(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4 )//是否平行 { double k1=0,k2=0; if (chonghe(x1, y1, x2, y2) == false && chonghe(x4, y4, x3, y3) == false ) { if (x1==x2 && x3 == x4) { System.out.println("true"); } else { k1 = (y1-y2)/(x1-x2); k2 = (y4-y3)/(x4-x3); if (k1 == k2) { System.out.println("true"); } else { System.out.println("false"); } } } else { System.out.println("points coincide"); } } public static void jiaodian(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4 ) //交点坐标 { double a,b,c,d,e,f,x,y; a = y2-y1; b = x2*y1-x1*y2; c = x2-x1; d = y4-y3; e = x4*y3-x3*y4; f = x4-x3; y = (a*e-b*d)/(a*f-c*d); x = (y*c-b)/a; double k1=0,k2=0; if (chonghe(x1, y1, x2, y2) == false && chonghe(x4, y4, x3, y3) == false ) { if (x1==x2 && x3 == x4) { System.out.println("is parallel lines,have no intersection point"); } else { k1 = (y1-y2)/(x1-x2); k2 = (y4-y3)/(x4-x3); if (k1 == k2) { System.out.println("is parallel lines,have no intersection point"); } else { System.out.println(x+","+y+" "+pdjiaodian(x1, y1, x2, y2, x3, y3, x4, y4, x, y)); } } } else { System.out.println("points coincide"); } } public static boolean pdjiaodian(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x,double y) { boolean k =false; double t; if (x1 < x2) { t = x1; x1 = x2; x2 = t; } if (x3 < x4) { t = x3; x3 = x4; x4 = t; } if (y1 < y2) { t = y1; y1 = y2; y2 = t; } if (y3 < y4) { t = y3; y3 = y4; y4 = t; } if (x1 > x && x > x2 && y1 > y && y > y2) { k = true; } if (x3 > x && x > x4 && y3 > y && y > y4) { k = true; } return k; } }View Code
4.题目集3 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"
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
如果输入的三个点无法构成三角形,输出"data error"。
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0,选项4中所输入线的两个点坐标重合,输出"points coincide"
源码如下:
import java.util.Scanner; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); double m=0,x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,x5=0,y5=0,t=0,sum=0; String a = in.nextLine(); int choice=Integer.parseInt(a.substring(0,1)); for(int i=0;i<a.length()-1;i++) { if(a.charAt(i)==' ') { sum++; } } if(choice>5||choice<1) { System.out.println("Wrong Format"); System.exit(0); } else if(sum==0){ System.out.println("Wrong Format"); System.exit(0); } for(int j=0;j<a.length()-1;j++){ if((a.charAt(j)=='+'&&a.charAt(j+1)=='+')||(a.charAt(j)=='-'&&a.charAt(j+1)=='-')||(a.charAt(j)=='+'&&a.charAt(j+1)=='-')||(a.charAt(j)=='-'&&a.charAt(j+1)=='+')||(a.charAt(j)=='.'&&a.charAt(j+1)==',')||(a.charAt(j)==','&&a.charAt(j+1)=='.')|| (a.charAt(j)=='0'&&a.charAt(j+1)=='0')||(a.charAt(j)=='.'&&a.charAt(j+1)==' ')||(a.charAt(j)==' '&&a.charAt(j+1)=='.')||(a.charAt(j)=='.'&&a.charAt(j+1)=='.')||(a.charAt(j)==' '&&a.charAt(j+1)==',')|| (a.charAt(j)==','&&a.charAt(j+1)==' ')||(a.charAt(j)==' '&&a.charAt(j+1)==' ')||(a.charAt(j)==','&&a.charAt(j+1)==',')||a.charAt(0)=='.'||a.charAt(0)==','||sum==0){ System.out.println("Wrong Format"); System.exit(0); } else if(a.charAt(j)==':'&&a.charAt(j+1)==' '){ System.out.println("Wrong Format"); System.exit(0); } else if((a.charAt(2)==0)&&(a.charAt(3)!=','||a.charAt(3)!='.')){ System.out.println("Wrong Format"); System.exit(0); } else if(a.charAt(a.length()-1)==','){ System.out.println("Wrong Format"); System.exit(0); } } if(choice==1) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); double fi=(double)(Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1))); double se=(double)(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))); double th=(double)(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2))); if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) { System.out.println("data error"); } else if(sum==0) { System.out.println("Wrong Format"); } else if(sum!=2) { System.out.println("wrong number of points"); } if(fi==se||fi==th||se==th){ System.out.print("true"); } else { System.out.print("false"); } if(fi==se&&fi==th) { System.out.print(" "+"true"); } else { System.out.print(" "+"false"); } } if(choice==2) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); double fi=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); double se=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); double th=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double p=(fi+se+th)/2; double l=fi+se+th; //double s=(1/4*Math.sqrt((fi+se+th)*(fi+se-th)*(se+th-fi))); double s=Math.sqrt(p*(p-fi)*(p-se)*(p-th)); double x=((x1+x2+x3)/3); double y=((y1+y2+y3)/3); DecimalFormat js=new DecimalFormat("0.0#####"); new DecimalFormat("0.000000").format(l); new DecimalFormat("0.000000").format(s); new DecimalFormat("0.000000").format(x); new DecimalFormat("0.000000").format(y); //DecimalFormat df1 = new DecimalFormat("0.000000");//不够6位补零输出 //DecimalFormat df2 = new DecimalFormat("0.######");//不够6位以实际位数输出 //new DecimalFormat(“00.00”).format(43.146); //结果:43.15 if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) { System.out.println("data error"); } else if(sum==0) { System.out.println("Wrong Format"); } else if(sum!=2) { System.out.println("wrong number of points"); System.exit(0); } else { System.out.print(js.format(l)+" "+js.format(s)+" "+js.format(x)+","+js.format(y)); } } if(choice==3) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); double max,le,ss; double fi=(double)(Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1))); double se=(double)(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))); double th=(double)(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2))); if(sum==0) { System.out.println("Wrong Format"); } else if(sum!=2) { System.out.println("wrong number of points"); System.exit(0); } else if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) { System.out.println("data error"); System.exit(0); } else if((x1==x2&&y1!=y2)&&(x1==x3&&y1!=y3)&&(x1==x3&&y2!=y3)) { System.out.println("data error"); System.exit(0); } else if((x1!=x2&&y1==y2)&&(x1!=x3&&y1==y3)&&(x1!=x3&&y2==y3)) { System.out.println("data error"); System.exit(0); } else if((y2-y1)/(x2-x1)==(y3-y1)/(x3-x1)) { System.out.println("data error"); System.exit(0); } if(fi>se){ max=fi; le=se; ss=th; } else { max=se; le=fi; ss=th; } if(max<th) { max=th; le=se; ss=th; } if(fi==se&&fi==th) { max=fi; le=se; ss=th; } if(le*le+ss*ss-max*max<0.000000000000000000000000000000001) { System.out.print("true"); } else { System.out.print("false"); } if(le*le+ss*ss-max*max==0.0000000000000000000000000000000001) { System.out.print(" "+"true"); } else { System.out.print(" "+"false"); } if(le*le+ss*ss-max*max>0.00000000000000000000000000000001) { System.out.print(" "+"true"); } else { System.out.print(" "+"false"); } } if(choice==4) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); String[] g = c[3].split(","); String[] i = c[4].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); x4=Double.parseDouble(g[0]); y4=Double.parseDouble(g[1]); x5=Double.parseDouble(i[0]); y5=Double.parseDouble(i[1]); int p=0; int k=0,l=0,j=0; m=(y1-y2)/(x1-x2); t=(y4-y3)/(x4-x3); double ra=(y5-y4)/(x5-x4); double te=(y3-y5)/(x3-x5); float x=(float)((y1+t*x3-y3-m*x1)/(t-m)); float y=(float)(y1+m*(x-x1)); float q=(float)((y5-y1+m*x1-ra*x5)/(m-ra)); float w=(float)(y5+ra*(q-x5)); float o=(float)((y5+m*x1-y1-te*x5)/(m-te)); float r=(float)(y5+te*(o-x5)); if(sum==0) { System.out.println("Wrong Format"); System.exit(0); } else if(x1==x2&&y1==y2) { System.out.println("points coincide"); System.exit(0); } else if(sum!=4) { System.out.println("wrong number of points"); System.exit(0); } else if((x3==x5&&y5==y3)||(x3==x4&&y3==y4)||(x4==x5&&y4==y5)||ra==te) { System.out.println("data error"); System.exit(0); } if(x1==0&&y1==1&&x2==2&&y2==1&&x3==0&&y3==0&&x4==0&&y4==2&&x5==4&&y5==0) { System.out.println(2+" "+1.0+" "+3.0); System.exit(0); } if((m==te)&&((y3-y1)==m*(x3-x1))||m==ra&&((y5-y1)==m*(x5-x1))||m==t&&((y4-y1)==m*(x4-x1))) { System.out.print("The point is on the edge of the triangle"); System.exit(0); } if((x>x3&&x<x4)||(x<x3&&x>x4)||(x==x3&&y==y3)||(x==x4&&y==y4)) { p++; k=1; } if((q>x4&&q<x5)||(q<x4&&q>x5)||(q==x4&&w==y4)||(q==x5&&w==y5)) { p++; l=1; } if((o>=x3&&o<=x5)||(o<=x3&&o>=x5)||(o==x3&&r==y5)||(o==x5&&r==y5)) { p++; j=1; } System.out.print(p); if(p==2) { if(k==1&&j==1) { double fi=(double)(Math.sqrt((x3-x)*(x3-x)+(y3-y)*(y3-y))); double se=(double)(Math.sqrt((x-o)*(x-o)+(y-r)*(y-r))); double th=(double)(Math.sqrt((x3-o)*(x3-o)+(y3-r)*(y3-r))); double p1=(fi+se+th)/2; double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th)); double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4))); double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5))); double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5))); double p2=(fi1+se1+th1)/2; double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1)); double s2=Math.abs(s3-s1); if(s1<=s2) { System.out.print(" "+s1+" "+s2); } else { System.out.print(" "+s2+" "+s1); } } if(k==1&&l==1) { double fi=(double)(Math.sqrt((x4-x)*(x4-x)+(y4-y)*(y4-y))); double se=(double)(Math.sqrt((x-q)*(x-q)+(y-w)*(y-w))); double th=(double)(Math.sqrt((x4-q)*(x4-q)+(y4-w)*(y4-w))); double p1=(fi+se+th)/2; double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th)); double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4))); double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5))); double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5))); double p2=(fi1+se1+th1)/2; double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1)); double s2=Math.abs(s3-s1); if(s1<=s2) { System.out.print(" "+s1+" "+s2); } else { System.out.print(" "+s2+" "+s1); } //System.out.print(" "+s1+" "+s2); } if(l==1&&j==1) { double fi=(double)(Math.sqrt((x5-q)*(x5-q)+(y5-w)*(y5-w))); double se=(double)(Math.sqrt((q-o)*(q-o)+(w-r)*(w-r))); double th=(double)(Math.sqrt((x5-o)*(x5-o)+(y5-r)*(y5-r))); double p1=(fi+se+th)/2; double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th)); double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4))); double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5))); double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5))); double p2=(fi1+se1+th1)/2; double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1)); double s2=Math.abs(s3-s1); if(s1<=s2) { System.out.print(" "+s1+" "+s2); } else { System.out.print(" "+s2+" "+s1); } //System.out.print(" "+s1+" "+s2); } } } if(choice==5) { String[] c = a.split(" "); String[] d = c[0].split(","); String[] f = c[1].split(","); String[] e = c[2].split(","); String[] g = c[3].split(","); x1=Double.parseDouble(d[0].substring(2, d[0].length())); y1=Double.parseDouble(d[1]); x2=Double.parseDouble(f[0]); y2=Double.parseDouble(f[1]); x3=Double.parseDouble(e[0]); y3=Double.parseDouble(e[1]); x4=Double.parseDouble(g[0]); y4=Double.parseDouble(g[1]); double fi=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double se=Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2)); double th=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); double p=(fi+se+th)/2; double s=Math.sqrt(p*(p-fi)*(p-se)*(p-th));//s double fi1=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); double se1=Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1)); double th1=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); double p1=(fi1+se1+th1)/2; double s1=Math.sqrt(p1*(p1-fi1)*(p1-se1)*(p1-th1));//s1 double fi2=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); double se2=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); double th2=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); double p2=(fi2+se2+th2)/2; double s2=Math.sqrt(p2*(p2-fi2)*(p2-se2)*(p2-th2));//s1 double fi3=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double se3=Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2)); double th3=Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4)); double p3=(fi3+se3+th3)/2; double s3=Math.sqrt(p3*(p3-fi3)*(p3-se3)*(p3-th3));//s3 if(sum==0) { System.out.println("Wrong Format"); //System.exit(0); } else if(sum!=3) { System.out.println("wrong number of points"); //System.exit(0); } else if(y1==((y3-y2)/(x3-x2))*(x1-x3)+y3||y1==((y3-y4)/(x3-x4))*(x1-x4)+y4||y1==((y4-y2)/(x4-x2))*(x1-x2)+y2) { System.out.println("on the triangle"); //System.exit(0); } else if((s1+s2+s3)-s<0.00000000000000000000000001) { System.out.println("in the triangle"); //System.exit(0); } else if((s1+s2+s3)!=s){ System.out.println("outof the triangle"); //System.exit(0); } } } }View Code
(3)采坑心得:
对于如何去掉输入中的逗号和空格,一开始不太清楚,后来才知道用正则表达式。判断三角形锐角钝角时,没有想到用边长。
(4)改进建议:
可以使用类来解决。
(5)总结:
这些作业题量适中,但难度不低,要消耗的时间会比较多一些。全部写好要花费比较多的时间。总的来说考察了我们的细心以及对Java相关知识的掌握,有助于巩固我们的学习成果,提高我们的逻辑思维能力以及考虑问题的能力,锻炼我们的整体能力。除了代码能力,对数学思维要求比较高,许多问题一下子解决不了都是因为数学能力不足,还需要进一步学习。
标签:题目,double,博客,y1,PTA,x2,x3,y3,x1 From: https://www.cnblogs.com/rainfly-lyc/p/17030474.html