PTA-Java-1
最近在PTA上进行了Java的编程作业,现在进行一些总结与归纳
- 题目详情
- 点线形系列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
输入样例:
整数输入。例如:
0,0 1,1
输出样例:
在这里给出相应的输出。例如:
1.4142135623730951
输入样例1:
带符号double类型实数输入。例如:
+2,-2.3 0.9,-3.2
输出样例1:
在这里给出相应的输出。例如:
1.42126704035519
输入样例2:
格式非法。例如:
1.42126704035519
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
输入样例3:
点的数量超过两个。例如:
+2,-2.3 0.9,-3.2 +2,-2.3
输出样例3:
在这里给出相应的输出。例如:
wrong number of points
代码详情:
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 double x1,y1,x2,y2; 8 String a = input.nextLine(); 9 String[] str = a.split(" "); 10 int i; 11 boolean k = true; 12 int len = str.length; 13 for (i = 0;i < len;i++) 14 { 15 k = func (str[i]); 16 if (k == false) 17 { 18 break; 19 } 20 } 21 if (k == true) 22 { 23 if(len == 2) 24 { 25 String[] str1 = str[0].split(","); 26 String[] str2 = str[1].split(","); 27 x1 = Double.valueOf(str1[0]); 28 y1 = Double.valueOf(str1[1]); 29 x2 = Double.valueOf(str2[0]); 30 y2 = Double.valueOf(str2[1]); 31 System.out.println(getDistance(x1,y1,x2,y2)); 32 } 33 else 34 { 35 System.out.println("wrong number of points"); 36 } 37 } 38 else 39 { 40 System.out.println("Wrong Format"); 41 42 } 43 44 45 } 46 public static boolean func(String d) 47 { 48 String[] a1 = d.split(","); 49 boolean flag,flag1; 50 String regex = "^([+-]?\\d+)(\\.\\d+)?"; 51 flag = a1[0].matches(regex); 52 flag1 = a1[1].matches(regex); 53 char[] b1 = new char[1000]; 54 char[] b2 = new char[1000]; 55 b1 = a1[0].toCharArray(); 56 b2 = a1[1].toCharArray(); 57 if (b1[0] == '0' && b1[1] == '0') 58 { 59 return false; 60 } 61 if (b2[0] == '0' && b2[1] == '0') 62 { 63 return false; 64 } 65 if (flag == false || flag1 == false ) 66 { 67 return false; 68 } 69 else 70 { 71 return true; 72 } 73 74 } 75 public static double getDistance(double x1,double y1,double x2,double y2){ 76 double r = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 77 return r; 78 } 79 }
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:
选项1,两点重合。例如:
1:-2,+5 -2,+5
输出样例:
在这里给出相应的输出。例如:
points coincide
输入样例2:
选项1,斜率无穷大的线。例如:
1:-2,3 -2,+5
输出样例:
在这里给出相应的输出。例如:
Slope does not exist
输入样例3:
选项1,斜率无穷大。例如:
1:-2,3 -2,+5
输出样例:
在这里给出相应的输出。例如:
Slope does not exist
输入样例4:
选项1,符合格式输入,带符号/不带符号数混合。例如:
1:-2.5,3 -2,+5.3
输出样例:
在这里给出相应的输出。例如:
4.6
输入样例5:
选项2,计算第一个点到另外两点连线的垂直距离。例如:
2:0,1 1,0 2,0
输出样例:
在这里给出相应的输出。例如:
1.0。
输入样例6:
选项3,判断三个点是否在一条线上。例如:
3:0,1 2,2 5,3
输出样例:
在这里给出相应的输出。例如:
false
输入样例7:
选项4,判断两条线是否平行。例如:
4:0,1 0,2 2,1 3,0
输出样例:
在这里给出相应的输出。例如:
false
输入样例8:
选项5,判断两条线的交点。例如:
5:0,0 -1,-1 0,2 3,-1
输出样例:
在这里给出相应的输出,交点坐标之间以英文","分隔,判断结果与坐标之间以一个英文空格分隔。例如:
1.0,1.0 true
输入样例9:
选项5,判断两条线的交点。但两条线平行例如:
5:0,0 -1,-1 2,3 3,4
输出样例:
在这里给出相应的输出,交点坐标之间以英文","分隔,判断结果与坐标之间以一个英文空格分隔。例如:
is parallel lines,have no intersection point
代码详情:
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 String str = input.nextLine(); 8 9 char[] a = new char[100]; 10 a = str.toCharArray(); 11 12 if (a[1] == ':') 13 { 14 String[] str1 = str.split(":"); 15 String[] str2 = str1[1].split(" "); 16 int i; 17 boolean k = true; 18 int len = str2.length; 19 double x1,y1,x2,y2,x3,y3,x4,y4; 20 for (i = 0;i < len;i++) 21 { 22 k = func(str2[i]); 23 if(k == false) 24 { 25 break; 26 } 27 } 28 switch (a[0]) 29 { 30 case '1':if(len == 2) 31 { 32 String[] s0 = str2[0].split(",");//x1,y1 33 String[] s1 = str2[1].split(",");//x2,y2 34 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); 35 x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); 36 xielv(x1, y1, x2, y2); 37 } 38 else 39 { 40 System.out.println("wrong number of points"); 41 } 42 break; 43 44 case '2':if(len == 3) 45 { 46 String[] s0 = str2[0].split(",");//x1,y1 47 String[] s1 = str2[1].split(",");//x2,y2 48 String[] s2 = str2[2].split(",");//x3,y3 49 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); 50 x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); 51 x3 = Double.valueOf(s2[0]);y3 = Double.valueOf(s2[1]); 52 juli(x1, y1, x2, y2, x3, y3); 53 } 54 else 55 { 56 System.out.println("wrong number of points"); 57 } 58 break; 59 60 case '3':if(len == 3) 61 { 62 String[] s0 = str2[0].split(",");//x1,y1 63 String[] s1 = str2[1].split(",");//x2,y2 64 String[] s2 = str2[2].split(",");//x3,y3 65 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); 66 x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); 67 x3 = Double.valueOf(s2[0]);y3 = Double.valueOf(s2[1]); 68 samezhixian(x1, y1, x2, y2, x3, y3); 69 } 70 else 71 { 72 System.out.println("wrong number of points"); 73 } 74 break; 75 76 case '4':if(len == 4) 77 { 78 String[] s0 = str2[0].split(",");//x1,y1 79 String[] s1 = str2[1].split(",");//x2,y2 80 String[] s2 = str2[2].split(",");//x3,y3 81 String[] s3 = str2[3].split(",");//x4,y4 82 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); 83 x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); 84 x3 = Double.valueOf(s2[0]);y3 = Double.valueOf(s2[1]); 85 x4 = Double.valueOf(s3[0]);y4 = Double.valueOf(s3[1]); 86 pinxing(x1, y1, x2, y2, x3, y3, x4, y4); 87 88 } 89 else 90 { 91 System.out.println("wrong number of points"); 92 } 93 break; 94 95 case '5':if(len == 4) 96 { 97 String[] s0 = str2[0].split(",");//x1,y1 98 String[] s1 = str2[1].split(",");//x2,y2 99 String[] s2 = str2[2].split(",");//x3,y3 100 String[] s3 = str2[3].split(",");//x4,y4 101 x1 = Double.valueOf(s0[0]);y1 = Double.valueOf(s0[1]); 102 x2 = Double.valueOf(s1[0]);y2 = Double.valueOf(s1[1]); 103 x3 = Double.valueOf(s2[0]);y3 = Double.valueOf(s2[1]); 104 x4 = Double.valueOf(s3[0]);y4 = Double.valueOf(s3[1]); 105 jiaodian(x1, y1, x2, y2, x3, y3, x4, y4); 106 } 107 else 108 { 109 System.out.println("wrong number of points"); 110 } 111 break; 112 default:System.out.println("Wrong Format");break; 113 } 114 } 115 else 116 { 117 System.out.println("Wrong Format"); 118 } 119 120 } 121 public static boolean func(String d) //格式是否正确 122 { 123 String[] a1 = d.split(","); 124 boolean flag =true; 125 String regex = "^([+-]?\\d+)(\\.\\d+)?"; 126 int i; 127 for (i = 0;i < a1.length;i++) 128 { 129 flag = a1[i].matches(regex); 130 if (flag == false) 131 { 132 break; 133 } 134 } 135 if (flag == false) 136 { 137 return false; 138 } 139 else 140 { 141 return true; 142 } 143 } 144 public static boolean chonghe(double x1,double y1,double x2,double y2)//是否重合 145 { 146 if (x1 == x2 && y1 == y2) 147 { 148 return true; 149 } 150 else 151 { 152 return false; 153 } 154 } 155 public static double xielv(double x1,double y1,double x2,double y2)//斜率,包括垂直 156 { 157 double k=0; 158 if (chonghe(x1, y1, x2, y2) == false) 159 { 160 if (x1 == x2 ) 161 { 162 System.out.println("Slope does not exist"); 163 } 164 else 165 { 166 k = (y1-y2)/(x1-x2); 167 System.out.println(k); 168 169 } 170 } 171 else 172 { 173 System.out.println("points coincide"); 174 } 175 return k; 176 } 177 public static double getDistance(double x1,double y1,double x2,double y2) //两点距离 178 { 179 double r = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 180 return r; 181 } 182 public static void juli(double x1,double y1,double x2,double y2,double x3,double y3)//第一点到另外两点直线距离 183 { 184 double len = getDistance(x2, y2, x3, y3); 185 double len1 = (x1*y2-x1*y3+x2*y3-x2*y1+x3*y1-x2*y2)/len; 186 187 if (chonghe(x2, y2, x3, y3) == false) 188 { 189 System.out.println(len1); 190 } 191 else 192 { 193 System.out.println("points coincide"); 194 } 195 } 196 public static void samezhixian(double x1,double y1,double x2,double y2,double x3,double y3)//是否同一条直线 197 { 198 boolean k = false; 199 double k1=0,k2=0; 200 if (chonghe(x1, y1, x2, y2) == false && chonghe(x1, y1, x3, y3) == false && chonghe(x2, y2, x3, y3) == false) 201 { 202 if(x1 == x2 && x2 == x3) 203 { 204 k = true; 205 System.out.println(k); 206 } 207 else 208 { 209 k1 = (y1-y2)/(x1-x2); 210 k2 = (y2-y3)/(x2-x3); 211 if (k1==k2) 212 { 213 k = true; 214 System.out.println(k); 215 } 216 else 217 { 218 System.out.println(k); 219 } 220 } 221 } 222 else 223 { 224 System.out.println("points coincide"); 225 } 226 } 227 public static void pinxing(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4 )//是否平行 228 { 229 double k1=0,k2=0; 230 if (chonghe(x1, y1, x2, y2) == false && chonghe(x4, y4, x3, y3) == false ) 231 { 232 if (x1==x2 && x3 == x4) 233 { 234 System.out.println("true"); 235 } 236 else 237 { 238 k1 = (y1-y2)/(x1-x2); 239 k2 = (y4-y3)/(x4-x3); 240 if (k1 == k2) 241 { 242 System.out.println("true"); 243 } 244 else 245 { 246 System.out.println("false"); 247 } 248 } 249 } 250 else 251 { 252 System.out.println("points coincide"); 253 } 254 } 255 public static void jiaodian(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4 ) //交点坐标 256 { 257 double a,b,c,d,e,f,x,y; 258 a = y2-y1; 259 b = x2*y1-x1*y2; 260 c = x2-x1; 261 d = y4-y3; 262 e = x4*y3-x3*y4; 263 f = x4-x3; 264 y = (a*e-b*d)/(a*f-c*d); 265 x = (y*c-b)/a; 266 267 double k1=0,k2=0; 268 if (chonghe(x1, y1, x2, y2) == false && chonghe(x4, y4, x3, y3) == false ) 269 { 270 if (x1==x2 && x3 == x4) 271 { 272 System.out.println("is parallel lines,have no intersection point"); 273 } 274 else 275 { 276 k1 = (y1-y2)/(x1-x2); 277 k2 = (y4-y3)/(x4-x3); 278 if (k1 == k2) 279 { 280 System.out.println("is parallel lines,have no intersection point"); 281 } 282 else 283 { 284 System.out.println(x+","+y+" "+pdjiaodian(x1, y1, x2, y2, x3, y3, x4, y4, x, y)); 285 } 286 } 287 } 288 else 289 { 290 System.out.println("points coincide"); 291 } 292 } 293 public static boolean pdjiaodian(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x,double y) 294 { 295 boolean k =false; 296 double t; 297 if (x1 < x2) 298 { 299 t = x1; 300 x1 = x2; 301 x2 = t; 302 } 303 if (x3 < x4) 304 { 305 t = x3; 306 x3 = x4; 307 x4 = t; 308 } 309 if (y1 < y2) 310 { 311 t = y1; 312 y1 = y2; 313 y2 = t; 314 } 315 if (y3 < y4) 316 { 317 t = y3; 318 y3 = y4; 319 y4 = t; 320 } 321 if (x1 > x && x > x2 && y1 > y && y > y2) 322 { 323 k = true; 324 } 325 if (x3 > x && x > x4 && y3 > y && y > y4) 326 { 327 k = true; 328 } 329 return k; 330 } 331 }
总结:
- 第一题输入数字,传入boolean类型的func方法中进行判断格式是否符合标准,采用正则表达式,定义regex,给regex进行赋值为
String regex = "^([+-]?\\d+)(\\.\\d+)?";
将输入的字符串传入func方法中进行校验,校验正确后将字符串切割为两个字符串,以“,”相隔,相当于两个坐标0,使用Double.valueOf()将字符串转化为浮点数进行运算