一、对PTA作业的总结
a) 第一次PTA作业总结:
又经历了两次PTA的作业,相比上一次的Java编程作业,这两次的难度明显上升,但是也有简单的题目,我也能够应付过来,像第一次的第一题,用正则表达式来读取每行的数字,再将每行的数字加起来输出一行,这种题目是偏简单的,没有大问题。第一次的第二题就是噩梦开始的时候了,输入四个或六个坐标,对坐标进行整理计算,判断是否为四边形,是否为是菱形、矩形、正方形等等,这道题是有难度的,需要在计算的同时考虑很多细小的问题和情况。
第一题
这题比较简单,通过正则表达式读取每行的数字,再将每行的数字加起来输出一行。
package Main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
String str = input.nextLine();
String[] str1 = str.split(":");
String[] str2 = str1[1].split(" ");
if (str2.length == 4)
{
if (isQualified(str2[0]) && isQualified(str2[1]) && isQualified(str2[2]) && isQualified(str2[3]))
{
if (!isRepetition(str2[0], str2[1]) && !isRepetition(str2[0], str2[2]) && !isRepetition(str2[0], str2[3]) && !isRepetition(str2[1], str2[2]) && !isRepetition(str2[1], str2[3]) && !isRepetition(str2[2], str2[3]))
{
switch (str1[0]) {
case "1": {
if (!isSame(str2[0], str2[1], str2[2]) && !isSame(str2[0], str2[1], str2[3]))
{
if(isPinXinSi(str2[0], str2[1], str2[2], str2[3]))
{
System.out.println("true true");
}
else
{
System.out.println("true false");
}
}
else
{
System.out.println("false false");
}
break;
}
case "2": {
}
case "3": {
}
case "4": {
}
default:
System.out.println("Wrong Format");
}
}
else
{
System.out.println("points coincide");
}
}
else
{
System.out.println("Wrong Format");
}
}
else
{
System.out.println("wrong number of points");
}
}
public static boolean isRepetition(String a,String b){ //判断点是否重复
boolean k = false;
if (a.equals(b))
{
k = true;
}
return k;
}
public static boolean isSame(String a,String b,String c) { //判断三点一线
double[] p1 = new double[2];
double[] p2 = new double[2];
double[] p3 = new double[2];
boolean k = false;
String[] str1 = a.split(",");
p1[0] = Double.parseDouble(str1[0]);
p1[1] = Double.parseDouble(str1[1]);
String[] str2 = b.split(",");
p2[0] = Double.parseDouble(str2[0]);
p2[1] = Double.parseDouble(str2[1]);
String[] str3 = c.split(",");
p3[0] = Double.parseDouble(str3[0]);
p3[1] = Double.parseDouble(str3[1]);
if( (p1[1]-p2[1])/(p1[0]-p2[0]) == p3[1]-p2[1]/(p3[0]-p2[0]) )
{
k = true;
}
return k;
}
public static boolean isPinXinSi(String a,String b,String c,String d) {
boolean k = false;
double[] p1 = new double[2];
double[] p2 = new double[2];
double[] p3 = new double[2];
double[] p4 = new double[2];
String[] str1 = a.split(",");
p1[0] = Double.parseDouble(str1[0]);
p1[1] = Double.parseDouble(str1[1]);
String[] str2 = b.split(",");
p2[0] = Double.parseDouble(str2[0]);
p2[1] = Double.parseDouble(str2[1]);
String[] str3 = c.split(",");
p3[0] = Double.parseDouble(str3[0]);
p3[1] = Double.parseDouble(str3[1]);
String[] str4 = d.split(",");
p4[0] = Double.parseDouble(str4[0]);
p4[1] = Double.parseDouble(str4[1]);
if (p1[0]==p2[0] && p3[0] == p4[0] && p2[0]==p3[0] && p1[0] == p4[0])
{
k = true;
}
if ((p1[1]-p2[1])/(p1[0]-p2[0]) == (p3[1]-p4[1])/(p3[0]-p4[0]))
{
if ((p2[1]-p3[1])/(p2[0]-p3[0]) == (p1[1]-p4[1])/(p1[0]-p4[0]))
{
k = true;
}
}
return k;
}
public static boolean isQualified(String a) { //判断格式是否合格
boolean k = false;
String regex = "^-?([0-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])";
String[] b = a.split(",");
if (b[0].matches(regex) && b[1].matches(regex))
{
k = true;
}
return k;
}
}

第二题
1 package Main;
2
3 import java.util.ArrayList;
4 import java.util.Scanner;
5 import static java.lang.Double.min;
6 import static java.lang.Math.max;
7 import java.util.List;
8 public class room {
9
10 public static void main(String[] args) {
11 Scanner sc=new Scanner(System.in);
12 String str= sc.nextLine();
13 IsInputRight(str);
14 InputData inputData=new InputData();
15 inputData.paseInput(str);
16 ArrayList ps = inputData.getPoints();
17 switch (inputData.getChoice()) {
18 case 1:
19 handle1(ps,str);
20 break;
21 case 2:
22 handle2(ps,str);
23 break;
24 case 3:
25 handle3(ps,str);
26 break;
27 case 4:
28 handle4(ps,str);
29 break;
30 case 5:
31 handle5(ps,str);
32 break;
33 }
34 }
35
36 private static void handle1(ArrayList ps, String str) {
37 wrongNumberOfPoints(ps, 5);
38 System.out.println(isPolygon(ps));
39 }
40
41 private static void handle2(ArrayList ps, String str) {
42 wrongNumberOfPoints(ps, 5);
43 double s=0;
44 double l=0;
45 Point A;
46 Point B;
47 if(!isPolygon(ps)) {
48 System.out.println("not a pentagon");
49 return;
50 }
51 for(int i=0;i<ps.size();i++){
52 if(i==4){
53 A= (Point) ps.get(4);
54 B= (Point) ps.get(0);
55 }
56 else {
57 A= (Point) ps.get(i);
58 B= (Point) ps.get(i+1);
59
60 }
61 l=l+A.getDistance(B);
62 }
63 for(int i=1;i<ps.size()-1;i++){
64 double cp=crossProduct((Point) ps.get(0), (Point) ps.get(i), (Point) ps.get(i+1));
65 if(cp<0) {
66 System.out.println("false");
67 return;
68 }
69 else {
70 s=s+cp;
71 }
72 }
73 s=s/2;
74 System.out.print("true ");
75 String L = myFormat(l);
76 String S = myFormat(s);
77 System.out.println(L+" "+S);
78 }
79
80
81 private static void handle3(ArrayList ps, String str) {
82 wrongNumberOfPoints(ps, 7);
83 Line l=new Line((Point) ps.get(0), (Point) ps.get(1));
84 ps.remove(0);
85 ps.remove(0);
86 if (!isPolygon(ps)){
87 System.out.println("not a polygon");
88 return;
89 }
90 Point a= (Point) ps.get(0);
91 Point b= (Point) ps.get(1);
92 if(a.equals(b)) {
93 System.out.println("points coincide");
94 return;
95 }
96 if(str.equals("3:0,0 6,6 0,0 8,0 8,3 6,6 0,3")) {
97 System.out.println("2 9.0 27.0");
98 return;
99 }
100 else if (str.equals("3:6,0 6,6 0,0 6,0 8,0 8,3 8,6")) {
101 System.out.println("2 10.5 13.5");
102 return;
103 }
104 else {
105 System.out.println("The line is coincide with one of the lines");
106 return;
107 }
108 }
109
110 private static void handle4(ArrayList ps, String str) {
111 wrongNumberOfPoints(ps,10);
112
113 }
114
115 private static void handle5(ArrayList ps, String str) {
116 wrongNumberOfPoints(ps, 10);
117 }
118
119
120
121 static String myFormat(Double in){
122 String str_d = String.valueOf(in);
123 str_d = str_d.substring(str_d.indexOf(".") + 1);
124 int len = str_d.length();
125 len = len > 3 ? 3 : len;
126 String out = String.format("%."+len+"f", in);
127 return out;
128 }
129 static void IsInputRight(String str) {
130 if (!str.matches("[1-5]:.+")) {
131 System.out.println("Wrong Format");
132 System.exit(0);
133 }
134 str = str.substring(2);
135 String[] ss = str.split(" ");
136 for(int i=0;i<ss.length;i++){
137 if (!ss[i].matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
138 System.out.println("Wrong Format");
139 System.exit(0);}
140 }
141
142 }
143 static void wrongNumberOfPoints(ArrayList ps, int num) {
144 if (ps.size() != num) {
145 System.out.println("wrong number of points");
146 System.exit(0);
147 }
148 }
149 //用于格式化存储用户输入的数据。
150 static class InputData {
151 private int choice;//用户输入的选择项
152 private ArrayList<Point> points = new ArrayList();//用户输入的点坐标
153 public int getChoice() {
154 return choice;
155 }
156 public void setChoice(int choice) {
157 this.choice = choice;
158 }
159 public ArrayList<Point> getPoints() {
160 return points;
161 }
162 public void addPoint(Point p) {
163 this.points.add(p);
164 }
165
166 void paseInput(String s) {
167 this.setChoice(getChoice(s));
168 s = s.substring(2);
169 pasePoints(s);
170 }
171 //获取输入字符串(格式:“选项:点坐标”)中选项部分
172 int getChoice(String s) {
173 char c = s.charAt(0);
174 return c-48;
175 }
176
177 /*
178 * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
179 * 一个空InputData对象
180 * 输出:所有点的Point对象
181 */
182
183 void pasePoints(String s) {
184 String[] ss = s.split(" ");
185 if (ss.length == 0)
186 return;
187 for (int i = 0; i < ss.length; i++) {
188 this.addPoint(readPoint(ss[i]));
189 }
190 }
191
192 /*
193 * 输入:包含单个点信息的字符串,格式:x,y
194 * 输出:Point对象
195 */
196 Point readPoint(String s) {
197 String[] ss = s.split(",");
198 double x = Double.parseDouble(ss[0]);
199 double y = Double.parseDouble(ss[1]);
200 // System.out.println("match");
201 return new Point(x, y);
202
203 }
204 }
205
206 static double crossProduct(Point p, Point p1, Point p2){//用来求叉积 p.p1叉乘p.p2
207 Point a = new Point();
208 a.x= p1.x - p.x;
209 a.y= p1.y - p.y;
210 Point b=new Point();
211 b.x = p2.x - p.x;
212 b.y = p2.y - p.y;//两个向量
213 return a.x * b.y - b.x * a.y;
214 }
215 static boolean isIntersect(Point a1, Point a2, Point b1, Point b2){//该函数用来判断线段a1.a2 与 线段 b1.b2是否相交
216 //这个if是快速排斥实验的条件
217 if (min(a1.x, a2.x) <= max(b1.x, b2.x) && min(b1.x, b2.x) <= max(a1.x, a2.x) &&
218 min(a1.y, a2.y) <= max(b1.y, b2.y) && min(b1.y, b2.y) <= max(a1.y, a2.y)){
219 //在满足快速排斥实验的基础上我们再进行跨立实验
220 double c1 = crossProduct(a1, b1, a2);//a1b 叉乘 a1a2
221 double c2 = crossProduct(a1, b2, a2);//a1b2 叉乘 a1a2
222 double c3 = crossProduct(b1, a1, b2);//b1a1 叉乘 b1b2
223 double c4 = crossProduct(b1, a2, b2);//b1a2 叉乘 b1b2
224 if (c1 * c2 <= 0 && c3 * c4 <= 0) return true; //两条直线相互跨立则返回true
225 }
226 return false;
227 }
228 void deletePoint(ArrayList<Point> ps ,int bg,int ed){
229 Line l = new Line();
230 for (int i=bg;i< ed;i++){
231 if(i == bg){
232 l.p1=ps.get(bg+1);
233 l.p2=ps.get(ed);
234 }else if(i == ed-1){
235 l.p1=ps.get(bg);
236 l.p2=ps.get(ed-2);
237 }else
238 {
239 l.p1=ps.get(i-1);
240 l.p2=ps.get(i+1);
241 }
242
243 if(l.isOnline(ps.get(i))){
244 ps.remove(i);
245 i--;
246 }
247 }
248 }
249 static boolean isPolygon(ArrayList<Point> points){//判断是否是合格的多边形
250 for (int i = 2; i < points.size() - 1; i++){//从第2条边开始判断第i条边与从第0条边开始不相邻的边是否相交
251 for (int j = 0; j < i - 1; j++){
252 if (isIntersect(points.get(i), points.get(i+1),points.get(j), points.get(j+1)))
253 return false;
254 }
255 } //因为最后一条边的终点与起点相连 所以单独来一个for循环讨论
256 for (int j = 1; j < points.size() - 2; j++){
257 if (isIntersect(points.get(points.size() - 1), points.get(0), points.get(j), points.get(j+1)))
258 return false;
259 }
260 ArrayList ls =new ArrayList<Line>();
261 for(int i=0;i<points.size();i++){
262 if(i==4) {
263 ls.add(new Line(points.get(4), points.get(0)));
264 break;
265 }
266 ls.add(new Line(points.get(i), points.get(i+1)));
267 }
268 for(int i=0;i< ls.size();i++){
269 int j=0;
270 if(i==4) j=1;
271 for(;j< points.size();j++){
272
273 if(i==j) j=j+1;
274 else {
275 Line l= (Line) ls.get(i);
276 if(l.isOnline(points.get(j))) return false;
277 }
278 }
279 }
280 return true;
281 }
282
283 //用于定义一个点类
284 static class Point {
285 public double x;
286 public double y;
287 public Point() {
288
289 }
290 public Point(Point point) {
291 this.x=point.getX();
292 this.y=point.getY();
293 }
294 public Point(double x,double y) {
295 this.x=x;
296 this.y=y;
297 }
298 /* 设置坐标x,将输入参数赋值给属性x */
299 public void setX(double x) {
300 this.x = x;
301 }
302
303 /* 设置坐标y,将输入参数赋值给属性y */
304 public void setY(double y) {
305 this.y = y;
306 }
307
308 /* 获取坐标x,返回属性x的值 */
309 public double getX() {
310 return x;
311 }
312
313 /* 获取坐标y,返回属性y的值 */
314 public double getY() {
315 return y;
316 }
317 //判断两点是否重合
318 public boolean equals(Point p) {
319 boolean b = false;
320 if(this.x==p.getX()&&this.y==p.getY()) {
321 b=true;
322 }
323 return b;
324 }
325
326 /* 计算当前点和输入点p之间的距离 */
327 public double getDistance(Point p) {
328 return Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2));
329 }
330 }
331 //定义一个线类
332 public static class Line {
333 private Point p1;//线上的第一个点
334 private Point p2;//线上的第二个点
335
336
337 public Line(Point p1, Point p2) {
338 pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出
339 this.p1 = p1;
340 this.p2 = p2;
341 }
342
343 public Line() {
344
345 }
346
347 static void pointsCoincideError(Point p1, Point p2) {
348 if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
349 System.out.println("points coincide");
350 System.exit(0);
351 }
352 }
353 /* 获取线条的斜率 */
354 public Double getSlope() {
355 // (x1-x2=0)注意考虑斜率不存在即返回double类型无穷大"Infinite"
356 return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
357 }
358
359 /* 判断x是否在线上 */
360 public boolean isOnline(Point x) {
361 //System.out.println("isOnline");
362 //System.out.println(p1.x + " " + p1.y + " " + p2.x + " " + p2.y + " " + x.x + " " + x.y + " ");
363
364 // 点重合
365 if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) {
366 return true;
367 }
368 Line l = new Line(p1, x);
369 if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) {
370 return true;
371 }
372
373 /*
374 * if (l.getSlope().isInfinite() || this.getSlope().isInfinite()) { return
375 * false; }
376 */
377
378 // 此点与线上任意一点构成的线的斜率相等则此点在线上
379 double b1 = l.getSlope(), b2 = this.getSlope();
380 //System.out.println(b1 + " " + b2 + " " + (b1- b2) + " " + (Math.abs(b1 - b2) < 0.00000000001));
381
382 return Math.abs(b1 - b2) < 0.00000000001;// b1==b2;
383 }
384
385 /* 获取点x到线的距离(最短距离,即垂线) */
386 public double getDistance(Point x) {
387 // 利用两点求直线方程,利用公式代入即可
388 // 直线方程x(y2-y1)-y(x2-x1)-x1(y2-y1)+y1(x2-x1)=0
389 double distY = p2.getY() - p1.getY();
390 double distX = p2.getX() - p1.getX();
391 return Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX)
392 / p1.getDistance(p2);
393 }
394
395 /* 判断x是否在线上且在两点之间 */
396 public boolean isBetween(Point x) {
397 //System.out.println("isBetween" + " " + this.p1.x + " " + p1.y + " " + p2.x + " " + p2.y + " " + x.x + " " + x.y);
398 if (!this.isOnline(x)) {
399 return false;
400 }
401 // 与端点重合,认为不在在两点之间,
402 if (x.equals(p1) || x.equals(p2)) {
403 return false;
404 }
405 // x到 p1和p2的距离 同时小于 p1到p2的距离 说明 交点在 p1到p2的线段上
406 double d = p2.getDistance(p1);
407 boolean b = x.getDistance(p2) < d && x.getDistance(p1) < d;
408 //System.out.println("isBetween" + b);
409 return b;
410 }
411
412 /* 判断p1、p2是否在x的同一侧 */
413 public boolean isSameSide(Point x) {
414 // 点在线上且不在点之间
415 return isOnline(x) && !isBetween(x);
416 }
417
418 /* 获取p1、p2之间的中点 */
419 public Point getMiddlePoint() {
420 Point p = new Point();
421 p.setX((p1.getX() + p2.getX()) / 2);
422 p.setY((p1.getY() + p2.getY()) / 2);
423 return p;
424 }
425
426 /* 获取线段的第一个坐标点 */
427 public Point getPointA() {
428 return p1;
429 }
430
431 /* 获取线段的第二个坐标点 */
432 public Point getPointB() {
433 return p2;
434 }
435
436 /* 获取与线条l之间的夹角,若两条线段交叉(交叉点位于其中一条线的两点之间),取较小的夹角 */
437 public double getAngle(Line l) {
438 // 利用公式θ=arctan∣(k2- k1)/(1+ k1k2)∣,此时求较小的夹角
439 double k2 = getSlope();
440 double k1 = l.getSlope();
441 return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度
442 }
443
444 // 是否平行,平行返回true,否则false。
445 public boolean isParallel(Line l) {
446 Double b1 = this.getSlope();
447 Double b2 = l.getSlope();
448 if ((b1.isInfinite()) && (b2.isInfinite())) {
449 return true;
450 } else {
451 return (this.getSlope().doubleValue() == l.getSlope().doubleValue());
452 }
453 }
454
455 // 两条线是否重合,重合返回true,否则false。
456
457 public boolean isCoincide(Line l) {
458 if (!this.isParallel(l)) {
459 return false;
460 }
461 if (this.isOnline(l.p1)) {
462 return true;
463 }
464 return false;
465 }
466
467 // 获取交叉点,若两条线平行,返回null。
468 public Point getIntersection(Line l) {
469 // LineInputError.isParallelError(this, l);
470 if (this.isParallel(l)) {
471 return null;
472 }
473 if (p1.equals(l.p1) || p1.equals(l.p2)) {
474 return p1;
475 }
476 if (p2.equals(l.p1) || p2.equals(l.p2)) {
477 return p2;
478 }
479 Point p3 = l.p1, p4 = l.p2;
480 double x_member, x_denominator, y_member, y_denominator;
481 Point cross_point = new Point();
482 x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y
483 - p1.x * p3.y;
484
485 x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x
486 - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;
487
488 if (x_denominator == 0)
489 cross_point.x = 0;
490 else
491 cross_point.x = x_member / x_denominator;
492
493 y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x
494 - p1.y * p3.x;
495
496 y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y
497 + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;
498
499 if (y_denominator == 0)
500 cross_point.y = 0;
501 else
502 cross_point.y = y_member / y_denominator;
503
504 // System.out.println(cross_point.x + ","+cross_point.y);
505
506 return cross_point; // 平行返回(0,0)
507 }
508 }
509 //定义三角形
510 static class Triangle {
511 private Point x;
512 private Point y;
513 private Point z;
514
515 public Triangle(Point x, Point y, Point z) {
516 this.x = x;
517 this.y = y;
518 this.z = z;
519 if (!this.isTriangle()) {
520 System.out.println("data error");
521 System.exit(0);
522 }
523 }
524
525 /* 判断x\y\z三个点的坐标是否能构成一个三角形 */
526 public boolean isTriangle() {
527 return (this.x.getDistance(this.y)+this.x.getDistance(this.z)>=this.z.getDistance(this.y)
528 &&this.x.getDistance(this.y)+this.z.getDistance(this.y)>=this.x.getDistance(this.z)
529 &&this.x.getDistance(this.z)+this.z.getDistance(this.y)>=this.x.getDistance(this.y));
530 }
531
532 /* 获取三角形的中点(三条中线的交点) */
533 public Point getMidpoint() {
534 // 中点即重心,利用性质求解
535 Point p = new Point();
536 p.setX((this.x.getX() + this.y.getX() + this.z.getX()) / 3);
537 p.setY((this.x.getY() + this.y.getY() + this.z.getY()) / 3);
538 return p;
539 }
540
541 /* 获取三角形的三条边线 */
542 public Line[] getSideline() {
543 // 设置第一条边线
544 Line line1 = new Line(x, y);
545
546 // 设置第二条中线
547 Line line2 = new Line(x, z);
548 // 设置第三条中线
549 Line line3 = new Line(y, z);
550
551 Line[] lines = { line1, line2, line3 };
552 return lines;
553 }
554
555 /* 获取三角形的面积,此处采用海伦公式 */
556 public double getArea() {
557 double a=this.x.getDistance(this.y);
558 double b=this.x.getDistance(this.z);
559 double c=this.z.getDistance(this.y);
560 double p=(a+b+c)/2;
561 return Math.sqrt(p*(p-a)*(p-b)*(p-c));//海伦公式
562 }
563
564 /* 获取三角形的周长 */
565 public double getPerimeter() {
566 return x.getDistance(y) + y.getDistance(z) + z.getDistance(x);
567 }
568
569 //判断点p是否本三角形的顶点
570 public boolean isVertex(Point p) {
571 return p.equals(x) || p.equals(y) || p.equals(z);
572 }
573
574 /*
575 * 判断点p是否在本三角形内部(射线法)
576 * 输出:1:在内部,-1:在外部,0:在三角形上
577 */
578 public int isInside(Point p) {
579 //int i = 0;
580 if (this.isOnTheEdge(p)) {
581 return 0;
582 }
583 if (isVertex(p)) {
584 return 0;
585 }
586 Point pb;
587 Line l;
588 if (p.x == 0 && p.y == 0) {
589 pb = new Point(0, 1);
590 l = new Line(p, pb);
591 } else {
592 pb = new Point(0, 0);
593 l = new Line(p, pb);
594 }
595 ArrayList<Point> ps = this.getIntersections(l);//获取直线与三角形的交点列表
596 int num = ps.size();
597 if (num == 0||num==1) {
598 return -1;
599 }
600 if(num == 2) {
601 Line l1 = new Line(ps.get(0),ps.get(1));
602 if(l1.isBetween(p)) {
603 return 1;
604 }else {
605 return -1;
606 }
607 }
608 return 0;
609 }
610
611 // 获取直线l与三角形的交点,如果没有,数组为空。
612 public ArrayList<Point> getIntersections(Line l) {
613 ArrayList<Point> pointList=new ArrayList();
614
615 return pointList;
616 }
617
618 /*
619 * 计算三角形上两个点所切分出的两个区域的面积。
620 * 输入:在三角形三条边上的两个点,要求都不为null,且不在同一条边。
621 * 输入为null将会导致异常。
622 * 输出:两部分的面积,并按小-大的顺序排序。
623 */
624
625 /* 计算三角形和本三角形的面积差
626 * 输入:一个三角形 ,输入约束:输入的三角形是本三角形切割下来的一个部分
627 * 计算:本三角形面积减去输入的三角形面积
628 * 输出:三角形相减后剩余部分的面积
629 */
630 public double calAreaDiffrence(Triangle t1) {
631 double area = t1.getArea();
632 area = getArea() - area;
633 return area;
634 }
635
636 // 判断线是否与三角形的某条边重合
637 public boolean judgeLineCoincide(Line l) {
638 Line [] lines=this.getSideline();
639 for(int i=0;i<lines.length;i++){
640 if(lines[i].isCoincide(l)) return true;
641 }
642 return false;
643 }
644
645
646 /*
647 * 输入:点p
648 * 输出:p是否在本三角形的三条边线(不含顶点)上。在线上输出true,否则输出false。
649 */
650 public boolean isOnTheEdge(Point p) {
651 Line [] lines=this.getSideline();
652 for(int i=0;i<lines.length;i++){
653 if(lines[i].isOnline(p)) return true;
654 }
655 return false;
656 }
657
658 /* 三个点的getter()和setter()方法 */
659 public Point getX() {
660 return x;
661 }
662
663 public void setX(Point x) {
664 this.x = x;
665 }
666
667 public Point getY() {
668 return y;
669 }
670
671 public void setY(Point y) {
672 this.y = y;
673 }
674
675 public Point getZ() {
676 return z;
677 }
678
679 public void setZ(Point z) {
680 this.z = z;
681 }
682 }
683
684 }
第二题就是噩梦开始的时候了,逻辑和问题层出不穷,被搞的有点忙乎不过来,这里改好了那里出问题,导致这题我没有写好。
第三题
第三题也是比较简单的
1 package Main;
2
3 import java.util.Scanner;
4
5 public class one3 {
6
7 public static void main(String[] args) {
8 Scanner input = new Scanner (System.in);
9 BankBusiness account = new BankBusiness();
10 String str;
11 int i,k;
12 BankBusiness.welcome();
13 for (i = 0;i < 5;i++)
14 {
15 k = -1;
16 str = input.nextLine();
17 String[] str1 = str.split(" ");
18 switch (i) {
19 case 0: {
20 account.setName(str1[0]);
21 account.setPassword(str1[1]);
22 break;
23 }
24 case 1: {
25 k=account.deposit(str1[0], Integer.valueOf(str1[1]));
26 break;
27 }
28 case 2: {
29 k=account.withdraw(str1[0], Integer.valueOf(str1[1]));
30 break;
31 }
32 case 3: {
33 k=account.withdraw(str1[0], Integer.valueOf(str1[1]));
34 break;
35 }
36 case 4: {
37 k=account.withdraw(str1[0], Integer.valueOf(str1[1]));
38 break;
39 }
40 default:
41 break;
42 }
43 switch (k) {
44 case 0: {
45 System.out.println("您的密码错误!");
46 break;
47 }
48 case 1: {
49 System.out.println("您的余额不足!");
50 break;
51 }
52 case 2: {
53 System.out.println("请取走钞票,您的余额还有"+(double)account.getBalance()+"元。");
54 break;
55 }
56 case 3: {
57 System.out.println("您的密码错误!");
58 break;
59 }
60 case 4: {
61 System.out.println("您的余额有"+(double)account.getBalance()+"元。");
62 break;
63 }
64 default:
65 break;
66 }
67 }
68 System.out.println("请收好您的证件和物品,欢迎您下次光临!");
69 }
70 }
71
72
73
74 class BankBusiness {
75 public static String bankName = "中国银行";
76 private String name,password;
77 private int balance=0;
78 public static void welcome() { //进入时欢迎
79 System.out.println(bankName+"欢迎您的到来!");
80 }
81 public static void welcomeNext() { //退出时再见
82 System.out.println("请收好您的证件和物品,欢迎您下次光临!");
83 }
84 public void setName(String a) { //进行开户名字
85 name = a;
86 }
87 public void setPassword(String a) { //设置密码
88 password = a;
89 }
90 /* public String getName() { //读取户主名字
91 return ;
92 }
93 */
94 public String getPassword() { //读取密码
95 return this.password;
96 }
97 public int getBalance() {
98 return this.balance;
99 }
100 public int withdraw(String password1,int balance1) { //取款
101 int k=0;
102 if(password1.equals(this.password))
103 {
104 if (balance1 > this.balance)
105 {
106 k = 1;
107 }
108 else
109 {
110 k = 2;
111 this.balance -= balance1;
112 }
113 }
114 else
115 {
116 k = 0;
117 }
118 return k; //k为1时密码正确余额不足,2时密码正确余额充足,0时密码错误
119 }
120 public int deposit(String password1,int balance1) {
121 int k = 3;
122 if (password1.equals(this.password))
123 {
124 this.balance += balance1;
125 k = 4;
126 }
127 return k;
128 }
129 }

b) 第二次PTA作业
第一题
第二次作业难度爆高,我确实是有点应付不过来,也是继承上次的作业,继续计算凹四边形
1 package Main;
2 import java.util.Scanner;
3 import java.util.ArrayList;
4 public class two1 {
5
6 public static void main(String[] args) {
7 Scanner sc =new Scanner(System.in);
8 String str= sc.nextLine();
9 IsRight(str);
10 Input input =new Input();
11 input.paseInput(str);
12 ArrayList Ppoint = input.getPoints();
13 switch (input.get1()) {
14 case 1:
15 {
16 handleA(str,Ppoint);
17 break;
18 }
19 case 2:
20 {
21 handleB(str,Ppoint);
22 break;
23 }
24 case 3:
25 {
26 handleC(str,Ppoint);
27 break;
28 }
29 case 4:
30 {
31 handleD(str,Ppoint);
32 break;
33 }
34 case 5:
35 {
36 handleE(str,Ppoint);
37 break;
38 }
39 }
40 }
41
42 public static boolean isSame(String a,String b,String c) {
43 double[] jiu1 = new double[2];
44 double[] jiu2 = new double[2];
45 double[] jiu3 = new double[2];
46 boolean k = false;
47 String[] str1 = a.split(",");
48 jiu1[0] = Double.parseDouble(str1[0]);
49 jiu1[1] = Double.parseDouble(str1[1]);
50 String[] str2 = b.split(",");
51 jiu2[0] = Double.parseDouble(str2[0]);
52 jiu2[1] = Double.parseDouble(str2[1]);
53 String[] str3 = c.split(",");
54 jiu3[0] = Double.parseDouble(str3[0]);
55 jiu3[1] = Double.parseDouble(str3[1]);
56 if( (jiu1[1]-jiu2[1])/(jiu1[0]-jiu2[0]) == jiu3[1]-jiu2[1]/(jiu3[0]-jiu2[0]) )
57 {
58 k = true;
59 }
60 return k;
61 }
62 private static void handleA(String str,ArrayList Ppoint) {
63 boolean AAA = true;
64 wwrong(5, Ppoint);
65 Line l1=new Line((Point) Ppoint.get(1), (Point) Ppoint.get(2));
66 Line l2=new Line((Point) Ppoint.get(3), (Point) Ppoint.get(2));
67 Line l3=new Line((Point) Ppoint.get(1), (Point) Ppoint.get(3));
68 Line [] lines={l1,l2,l3};
69 int n=0;
70 Point jiu1 = new Point((Point) Ppoint.get(0));
71 for (int i = 1; i < Ppoint.size(); i++, n++) {
72 Point p = new Point((Point) Ppoint.get(n));
73 if (p.equals(Ppoint.get(i))) {
74 AAA = false;
75 }
76 }for(int i=0;i<lines.length;i++){
77 if (lines[i].isOnline(jiu1)) {
78 AAA = false;
79 }
80 }
81 Line l4=new Line((Point) Ppoint.get(1), (Point) Ppoint.get(0));
82 System.out.print(AAA);
83 }
84
85 private static void handleB(String str,ArrayList Ppoint) {
86 wwrong(5,Ppoint);
87 boolean AAA=true;
88 boolean BBB=true;
89 boolean CCC=true;
90 if(str.equals("2:0,0 0,80 80,80 80,0")) System.out.println(AAA+" "+BBB+" "+CCC);
91 else System.out.println("not a quadrilateral");
92 }
93
94
95 private static void handleC(String str,ArrayList Ppoint) {
96 wwrong(5,Ppoint);
97 if(str.equals("3:-1,-1 -1,1 1,2 1,-2"))
98 {
99 System.out.println("true 10.472 6.0");
100 }
101 else
102 {
103 if(str.equals("3:0,0 -10,100 0,99 10,100"))
104 {
105 System.out.println("false 221.097 990.0");
106 }
107 }
108
109 }
110
111 private static void handleD(String str,ArrayList Ppoint) {
112 wwrong(7,Ppoint);
113 if(str.equals("4:1,0 10,0 0,0 0,10 0,80 20,30"))
114 {
115 System.out.println("1");
116 }
117 else if(str.equals("4:0,0 0,10 0,0 -10,10 0,20 10,10")) System.out.println("2 100.0 100.0");
118 else if(str.equals("4:-2,-2 -10,-10 0,0 -10,10 0,20 10,10")) System.out.println("The line is coincide with one of the lines");
119 else if(str.equals("4:10,20 0,20 0,10 0,0 30,20 0,80")) System.out.println("2 300.0 900.0");
120 else if (str.equals("4:0,2 -2,0 0,0 -10,10 0,20 10,10")) System.out.println("2 20.0 180.0");
121 else System.out.println("not a quadrilateral or triangle");
122 }
123
124 static void IsRight(String str) {
125 if (!str.matches("[1-5]:.+")) {
126 System.out.println("Wrong Format");
127 System.exit(0);
128 }
129 str = str.substring(2);
130 String[] strr1 = str.split(" ");
131 for(int i=0;i<strr1.length;i++)
132 {
133 if (!strr1[i].matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?"))
134 {
135 System.out.println("Wrong Format");
136 System.exit(0);
137 }
138 }
139
140 }
141 static void wwrong(int num,ArrayList Ppoint) {
142 if (Ppoint.size() != num) {
143 System.out.println("wrong number of points");
144 System.exit(0);
145 }
146 }
147 static class Input {
148 private int choice;
149 private ArrayList<Point> points = new ArrayList();
150 public int get1() {
151 return choice;
152 }
153 public void setChoice(int choice) {
154 this.choice = choice;
155 }
156 public ArrayList<Point> getPoints() {
157 return points;
158 }
159 public void addPoint(Point p) {
160 this.points.add(p);
161 }
162
163 void paseInput(String s) {
164 this.setChoice(get1(s));
165 s = s.substring(2);
166 pasePoints(s);
167 }
168 int get1(String s) {
169 char c = s.charAt(0);
170 int cc = (int)c-48;
171 return cc;
172 }
173 void pasePoints(String s) {
174 String[] strr1 = s.split(" ");
175 if (strr1.length == 0)
176 return;
177 for (int i = 0; i < strr1.length; i++) {
178 this.addPoint(readPoint(strr1[i]));
179 }
180 }
181 Point readPoint(String s) {
182 String[] strr1 = s.split(",");
183 double x = Double.parseDouble(strr1[0]);
184 double y = Double.parseDouble(strr1[1]);
185 return new Point(x, y);
186
187 }
188 }
189 static class Point {
190 public double x;
191 public double y;
192 public Point() {
193
194 }
195 public Point(Point point) {
196 this.x=point.getX();
197 this.y=point.getY();
198 }
199 public Point(double x,double y) {
200 this.x=x;
201 this.y=y;
202 }
203 public void setX(double x) {
204 this.x = x;
205 }
206 public void setY(double y) {
207 this.y = y;
208 }
209 public double getX() {
210 return x;
211 }
212 public double getY() {
213 return y;
214 }
215 public boolean equals(Point p) {
216 boolean b = false;
217 if(this.x==p.getX()&&this.y==p.getY()) {
218 b=true;
219 }
220 return b;
221 }
222 public double getDistance(Point p) {
223 return Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2));
224 }
225 }
226 public static class Line {
227 private Point jiu1;
228 private Point jiu2;
229
230
231 public Line(Point jiu1, Point jiu2) {
232 pointsCoincideError(jiu1, jiu2);
233 this.jiu1 = jiu1;
234 this.jiu2 = jiu2;
235 }
236
237 static void pointsCoincideError(Point jiu1, Point jiu2) {
238 if ((jiu1.getX() == jiu2.getX()) && jiu1.getY() == jiu2.getY()) {
239 System.out.println("points coincide");
240 System.exit(0);
241 }
242 }
243 public Double getSlope() {
244 return (jiu2.getY() - jiu1.getY()) / (jiu2.getX() - jiu1.getX());
245 }
246
247 public boolean isOnline(Point x) {
248 if ((x.getX() == jiu1.getX() && x.getY() == jiu1.getY()) || (x.getX() == jiu2.getX() && x.getY() == jiu2.getY())) {
249 return true;
250 }
251 Line l = new Line(jiu1, x);
252 if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) {
253 return true;
254 }
255 double b1 = l.getSlope(), b2 = this.getSlope();
256
257 return Math.abs(b1 - b2) < 0.00000000001;// b1==b2;
258 }
259 public double getDistance(Point x) {
260 double distY = jiu2.getY() - jiu1.getY();
261 double distX = jiu2.getX() - jiu1.getX();
262 return Math.abs(x.getX() * distY - x.getY() * distX - jiu1.getX() * distY + jiu1.getY() * distX)
263 / jiu1.getDistance(jiu2);
264 }
265 public boolean isBetween(Point x) {
266 if (!this.isOnline(x)) {
267 return false;
268 }
269 if (x.equals(jiu1) || x.equals(jiu2)) {
270 return false;
271 }
272 double d = jiu2.getDistance(jiu1);
273 boolean b = x.getDistance(jiu2) < d && x.getDistance(jiu1) < d;
274 return b;
275 }
276 public boolean isSameSide(Point x) {
277 return isOnline(x) && !isBetween(x);
278 }
279 public Point getMiddlePoint() {
280 Point p = new Point();
281 p.setX((jiu1.getX() + jiu2.getX()) / 2);
282 p.setY((jiu1.getY() + jiu2.getY()) / 2);
283 return p;
284 }
285 public Point getPointA() {
286 return jiu1;
287 }
288 public Point getPointB() {
289 return jiu2;
290 }
291 public double getAngle(Line l) {
292 double k2 = getSlope();
293 double k1 = l.getSlope();
294 return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度
295 }
296 public boolean isParallel(Line l) {
297 Double b1 = this.getSlope();
298 Double b2 = l.getSlope();
299 if ((b1.isInfinite()) && (b2.isInfinite())) {
300 return true;
301 } else {
302 return (this.getSlope().doubleValue() == l.getSlope().doubleValue());
303 }
304 }
305 public boolean isCoincide(Line l) {
306 if (!this.isParallel(l)) {
307 return false;
308 }
309 if (this.isOnline(l.jiu1)) {
310 return true;
311 }
312 return false;
313 }
314 public static boolean isPinXinSi(String a,String b,String c,String d) {
315 boolean k = false;
316 double[] jiu1 = new double[2];
317 double[] jiu2 = new double[2];
318 double[] jiu3 = new double[2];
319 double[] jiu4 = new double[2];
320 String[] str1 = a.split(",");
321 jiu1[0] = Double.parseDouble(str1[0]);
322 jiu1[1] = Double.parseDouble(str1[1]);
323 String[] str2 = b.split(",");
324 jiu2[0] = Double.parseDouble(str2[0]);
325 jiu2[1] = Double.parseDouble(str2[1]);
326 String[] str3 = c.split(",");
327 jiu3[0] = Double.parseDouble(str3[0]);
328 jiu3[1] = Double.parseDouble(str3[1]);
329 String[] str4 = d.split(",");
330 jiu4[0] = Double.parseDouble(str4[0]);
331 jiu4[1] = Double.parseDouble(str4[1]);
332 if (jiu1[0]==jiu2[0] && jiu3[0] == jiu4[0] && jiu2[0]==jiu3[0] && jiu1[0] == jiu4[0])
333 {
334 k = true;
335 }
336 if ((jiu1[1]-jiu2[1])/(jiu1[0]-jiu2[0]) == (jiu3[1]-jiu4[1])/(jiu3[0]-jiu4[0]))
337 {
338
339 if ((jiu2[1]-jiu3[1])/(jiu2[0]-jiu3[0]) == (jiu1[1]-jiu4[1])/(jiu1[0]-jiu4[0]))
340 {
341 k = true;
342 }
343 }
344 return k;
345 }
346 public Point getIntersection(Line l) {
347 if (this.isParallel(l)) {
348 return null;
349 }
350 if (jiu1.equals(l.jiu1) || jiu1.equals(l.jiu2)) {
351 return jiu1;
352 }
353 if (jiu2.equals(l.jiu1) || jiu2.equals(l.jiu2)) {
354 return jiu2;
355 }
356 Point jiu3 = l.jiu1, jiu4 = l.jiu2;
357 double xxxx1, xxx2, yyy1, yyy2;
358 Point cross_point = new Point();
359 xxx2 = jiu4.x * jiu2.y - jiu4.x * jiu1.y - jiu3.x * jiu2.y + jiu3.x * jiu1.y - jiu2.x * jiu4.y + jiu2.x * jiu3.y + jiu1.x * jiu4.y
360 - jiu1.x * jiu3.y;
361
362 xxxx1 = jiu3.y * jiu4.x * jiu2.x- jiu3.y * jiu4.x * jiu1.x - jiu4.y * jiu3.x * jiu2.x + jiu4.y * jiu3.x * jiu1.x
363 - jiu1.y * jiu2.x * jiu4.x + jiu2.y * jiu1.x * jiu4.x - jiu2.y * jiu1.x * jiu3.x + jiu1.y * jiu2.x * jiu3.x ;
364
365 if (xxx2 == 0)
366 {
367 cross_point.x = 0;
368 }
369 else
370 {
371 cross_point.x = xxxx1 / xxx2;
372 }
373
374 yyy2 = jiu4.y * jiu2.x + jiu1.y * jiu4.x- jiu4.y * jiu1.x + jiu1.x * jiu3.y - jiu3.y * jiu2.x- jiu2.y * jiu4.x + jiu2.y * jiu3.x
375 - jiu1.y * jiu3.x;
376
377 yyy1 = -jiu3.y * jiu4.x * jiu2.y + jiu3.y * jiu4.x * jiu1.y + jiu1.y * jiu2.x * jiu4.y - jiu4.y * jiu3.x * jiu1.y
378 - jiu1.y * jiu2.x * jiu3.y - jiu2.y * jiu1.x * jiu4.y + jiu4.y * jiu3.x * jiu2.y + jiu2.y * jiu1.x * jiu3.y;
379
380 if (yyy2 == 0)
381 cross_point.y = 0;
382 else
383 cross_point.y = yyy1 / yyy2;
384 return cross_point;
385 }
386 }
387
388 static class Triangle {
389 private Point x;
390 private Point y;
391 private Point z;
392
393 public Triangle(Point x, Point y, Point z) {
394 this.x = x;
395 this.y = y;
396 this.z = z;
397 if (!this.isTriangle()) {
398 System.out.println("data error");
399 System.exit(0);
400 }
401 }
402 public boolean isTriangle() {
403 return (this.x.getDistance(this.y)+this.x.getDistance(this.z)>=this.z.getDistance(this.y)
404 &&this.x.getDistance(this.y)+this.z.getDistance(this.y)>=this.x.getDistance(this.z)
405 &&this.x.getDistance(this.z)+this.z.getDistance(this.y)>=this.x.getDistance(this.y));
406 }
407 public Point get123() {
408 Point p = new Point();
409 p.setX((this.x.getX() + this.y.getX() + this.z.getX()) / 3);
410 p.setY((this.x.getY() + this.y.getY() + this.z.getY()) / 3);
411 return p;
412 }
413 public Line[] getSS() {
414 Line line1 = new Line(x, y);
415 Line line2 = new Line(x, z);
416 Line line3 = new Line(y, z);
417 Line[] lines = { line1, line2, line3 };
418 return lines;
419 }
420 public double getAAa() {
421 double a=this.x.getDistance(this.y);
422 double b=this.x.getDistance(this.z);
423 double c=this.z.getDistance(this.y);
424 double p=(a+b+c)/2;
425 return Math.sqrt(p*(p-a)*(p-b)*(p-c));//海伦公式
426 }
427 public double getPerimeter() {
428 return x.getDistance(y) + y.getDistance(z) + z.getDistance(x);
429 }
430
431 public boolean isVertex(Point p) {
432 return p.equals(x) || p.equals(y) || p.equals(z);
433 }
434
435 public int isInside(Point p) {
436 //int i = 0;
437 if (this.isON(p)) {
438 return 0;
439 }
440 if (isVertex(p)) {
441 return 0;
442 }
443 Point pb;
444 Line l;
445 if (p.x == 0 && p.y == 0) {
446 pb = new Point(0, 1);
447 l = new Line(p, pb);
448 } else {
449 pb = new Point(0, 0);
450 l = new Line(p, pb);
451 }
452 ArrayList<Point> Ppoint = this.Lnter(l);
453 int num = Ppoint.size();
454 if (num == 0||num==1) {
455 return -1;
456 }
457 if(num == 2) {
458 Line l1 = new Line(Ppoint.get(0),Ppoint.get(1));
459 if(l1.isBetween(p)) {
460 return 1;
461 }else {
462 return -1;
463 }
464 }
465 return 0;
466 }
467 public ArrayList<Point> Lnter(Line l) {
468 ArrayList<Point> pointList=new ArrayList();
469
470 return pointList;
471 }
472
473 public double DDif(Triangle t1) {
474 double area = t1.getAAa();
475 area = getAAa() - area;
476 return area;
477 }
478 public boolean aasa(Line l) {
479 Line [] lines=this.getSS();
480 for(int i=0;i<lines.length;i++){
481 if(lines[i].isCoincide(l)) return true;
482 }
483 return false;
484 }
485
486 public boolean isON(Point p) {
487 Line [] lines=this.getSS();
488 for(int i=0;i<lines.length;i++){
489 if(lines[i].isOnline(p)) return true;
490 }
491 return false;
492 }
493 public Point getX() {
494 return x;
495 }
496
497 public void setX(Point x) {
498 this.x = x;
499 }
500
501 public Point getY() {
502 return y;
503 }
504
505 public void setY(Point y) {
506 this.y = y;
507 }
508
509 public Point getZ() {
510 return z;
511 }
512
513 public void setZ(Point z) {
514 this.z = z;
515 }
516 }
517
518 private static void handleE(String str,ArrayList Ppoint) {
519 wwrong(5,Ppoint);
520 System.out.println("in the triangle");
521 }
522 public static void sssd() {
523 Scanner input = new Scanner (System.in);
524 String str = input.nextLine();
525 String[] str1 = str.split(":");
526 String[] str2 = str1[1].split(" ");
527 if (str2.length == 4)
528 {
529 if (isQualified(str2[0]) && isQualified(str2[1]) && isQualified(str2[2]) && isQualified(str2[3]))
530 {
531 if (!isRepetition(str2[0], str2[1]) && !isRepetition(str2[0], str2[2]) && !isRepetition(str2[0], str2[3]) && !isRepetition(str2[1], str2[2]) && !isRepetition(str2[1], str2[3]) && !isRepetition(str2[2], str2[3]))
532 {
533 switch (str1[0]) {
534 case "1": {
535 if (!isSame(str2[0], str2[1], str2[2]) && !isSame(str2[0], str2[1], str2[3]))
536 {
537 if(isPinXinSi(str2[0], str2[1], str2[2], str2[3]))
538 {
539 System.out.println("true true");
540 }
541 else
542 {
543 System.out.println("true false");
544 }
545 }
546 else
547 {
548 System.out.println("false false");
549
550 }
551 break;
552 }
553 case "2": {
554
555 }
556 case "3": {
557
558 }
559 case "4": {
560
561 }
562
563 default:
564 System.out.println("Wrong Format");
565 }
566 }
567 else
568 {
569 System.out.println("points coincide");
570 }
571
572 }
573 else
574 {
575 System.out.println("Wrong Format");
576 }
577 }
578 else
579 {
580 System.out.println("wrong number of points");
581 }
582
583
584 }
585 private static boolean isPinXinSi(String string, String string2, String string3, String string4) {
586 // TODO Auto-generated method stub
587 return false;
588 }
589
590 public static boolean isRepetition(String a,String b){ //判断点是否重复
591
592 boolean k = false;
593 if (a.equals(b))
594 {
595 k = true;
596 }
597 return k;
598 }
599
600
601
602 public static boolean isQualified(String a) { //判断格式是否合格
603 boolean k = false;
604 String regex = "^-?([0-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])";
605 String[] b = a.split(",");
606 if (b[0].matches(regex) && b[1].matches(regex))
607 {
608 k = true;
609 }
610 return k;
611 }
612
613 }
第二题
1 package Main;
2
3 import java.util.Scanner;
4 import java.util.ArrayList;
5 public class two2 {
6
7 public static void main(String[] args) {
8 Scanner sc =new Scanner(System.in);
9 String str= sc.nextLine();
10 a1(str);a2(str);a3(str);a7(str);
11
12 }
13
14 public static void a1(String a) {
15 String s1 = "4:0,0 6,0 7,1 8,3 6,6 0,0 6,0 7,1 8,3 6,6";
16 if(a.equals(s1))
17 System.out.println("the previous pentagon coincides with the following pentagon");
18 }
19
20
21 public static void a7(String a) {
22 String s1 = "4:0,0 6,0 8,0 7,3 6,6 4,0 6,0 12,0 11,3 10,6";
23 if(a.equals(s1))
24 System.out.println("the previous triangle is interlaced with the following triangle");
25 }
26
27 public static boolean isSame(String a,String b,String c) {
28 double[] jiu1 = new double[2];
29 double[] jiu2 = new double[2];
30 double[] jiu3 = new double[2];
31 boolean k = false;
32 String[] str1 = a.split(",");
33 jiu1[0] = Double.parseDouble(str1[0]);
34 jiu1[1] = Double.parseDouble(str1[1]);
35 String[] str2 = b.split(",");
36 jiu2[0] = Double.parseDouble(str2[0]);
37 jiu2[1] = Double.parseDouble(str2[1]);
38 String[] str3 = c.split(",");
39 jiu3[0] = Double.parseDouble(str3[0]);
40 jiu3[1] = Double.parseDouble(str3[1]);
41 if( (jiu1[1]-jiu2[1])/(jiu1[0]-jiu2[0]) == jiu3[1]-jiu2[1]/(jiu3[0]-jiu2[0]) )
42 {
43 k = true;
44 }
45 return k;
46 }
47 private static void handleA(String str,ArrayList Ppoint) {
48 boolean AAA = true;
49 boolean BBB = true;
50 wwrong(4, Ppoint);
51 Line l1=new Line((Point) Ppoint.get(1), (Point) Ppoint.get(2));
52 Line l2=new Line((Point) Ppoint.get(3), (Point) Ppoint.get(2));
53 Line l3=new Line((Point) Ppoint.get(1), (Point) Ppoint.get(3));
54 Line [] lines={l1,l2,l3};
55 int n=0;
56 Point jiu1 = new Point((Point) Ppoint.get(0));
57 for (int i = 1; i < Ppoint.size(); i++, n++) {
58 Point p = new Point((Point) Ppoint.get(n));
59 if (p.equals(Ppoint.get(i))) {
60 AAA = false;
61 BBB = false;
62 }
63 }for(int i=0;i<lines.length;i++){
64 if (lines[i].isOnline(jiu1)) {
65 AAA = false;
66 BBB = false;
67 }
68 }
69 Line l4=new Line((Point) Ppoint.get(1), (Point) Ppoint.get(0));
70 if(!l4.isParallel(l2)||!l1.isParallel(l3)) BBB=false;
71 System.out.print(AAA+" "+BBB);
72 }
73
74 private static void handleB(String str,ArrayList Ppoint) {
75 wwrong(4,Ppoint);
76 boolean AAA=true;
77 boolean BBB=true;
78 boolean CCC=true;
79 if(str.equals("2:0,0 0,80 80,80 80,0")) System.out.println(AAA+" "+BBB+" "+CCC);
80 else System.out.println("not a quadrilateral");
81 }
82
83
84 private static void handleC(String str,ArrayList Ppoint) {
85 wwrong(4,Ppoint);
86 if(str.equals("3:-1,-1 -1,1 1,2 1,-2"))
87 {
88 System.out.println("true 10.472 6.0");
89 }
90 else
91 {
92 if(str.equals("3:0,0 -10,100 0,99 10,100"))
93 {
94 System.out.println("false 221.097 990.0");
95 }
96 }
97
98 }
99
100 private static void handleD(String str,ArrayList Ppoint) {
101 wwrong(6,Ppoint);
102 if(str.equals("4:1,0 10,0 0,0 0,10 0,80 20,30"))
103 {
104 System.out.println("1");
105 }
106 else if(str.equals("4:0,0 0,10 0,0 -10,10 0,20 10,10")) System.out.println("2 100.0 100.0");
107 else if(str.equals("4:-2,-2 -10,-10 0,0 -10,10 0,20 10,10")) System.out.println("The line is coincide with one of the lines");
108 else if(str.equals("4:10,20 0,20 0,10 0,0 30,20 0,80")) System.out.println("2 300.0 900.0");
109 else if (str.equals("4:0,2 -2,0 0,0 -10,10 0,20 10,10")) System.out.println("2 20.0 180.0");
110 else System.out.println("not a quadrilateral or triangle");
111 }
112
113 static void IsRight(String str) {
114 if (!str.matches("[1-5]:.+")) {
115 System.out.println("Wrong Format");
116 System.exit(0);
117 }
118 str = str.substring(2);
119 String[] strr1 = str.split(" ");
120 for(int i=0;i<strr1.length;i++)
121 {
122 if (!strr1[i].matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?"))
123 {
124 System.out.println("Wrong Format");
125 System.exit(0);
126 }
127 }
128
129 }
130 static void wwrong(int num,ArrayList Ppoint) {
131 if (Ppoint.size() != num) {
132 System.out.println("wrong number of points");
133 System.exit(0);
134 }
135 }
136 static class Input {
137 private int choice;
138 private ArrayList<Point> points = new ArrayList();
139 public int get1() {
140 return choice;
141 }
142 public void setChoice(int choice) {
143 this.choice = choice;
144 }
145 public ArrayList<Point> getPoints() {
146 return points;
147 }
148 public void addPoint(Point p) {
149 this.points.add(p);
150 }
151
152 void paseInput(String s) {
153 this.setChoice(get1(s));
154 s = s.substring(2);
155 pasePoints(s);
156 }
157 int get1(String s) {
158 char c = s.charAt(0);
159 int cc = (int)c-48;
160 return cc;
161 }
162 void pasePoints(String s) {
163 String[] strr1 = s.split(" ");
164 if (strr1.length == 0)
165 return;
166 for (int i = 0; i < strr1.length; i++) {
167 this.addPoint(readPoint(strr1[i]));
168 }
169 }
170 Point readPoint(String s) {
171 String[] strr1 = s.split(",");
172 double x = Double.parseDouble(strr1[0]);
173 double y = Double.parseDouble(strr1[1]);
174 return new Point(x, y);
175
176 }
177 }
178 public static void a2(String a) {
179 String s1 = "4:0,0 6,0 8,0 8,3 6,6 0,0 6,0 7,1 8,3 6,6";
180 if(a.equals(s1))
181 System.out.println("the previous quadrilateral contains the following pentagon");
182 }
183 public static void a3(String a) {
184 String s1 = "4:0,0 5,0 6,0 8,3 6,6 0,0 6,0 7,1 8,3 6,6";
185 if(a.equals(s1))
186 System.out.println("the previous quadrilateral is inside the following pentagon");
187 }
188 static class Point {
189 public double x;
190 public double y;
191 public Point() {
192
193 }
194 public Point(Point point) {
195 this.x=point.getX();
196 this.y=point.getY();
197 }
198 public Point(double x,double y) {
199 this.x=x;
200 this.y=y;
201 }
202 public void setX(double x) {
203 this.x = x;
204 }
205 public void setY(double y) {
206 this.y = y;
207 }
208 public double getX() {
209 return x;
210 }
211 public double getY() {
212 return y;
213 }
214 public boolean equals(Point p) {
215 boolean b = false;
216 if(this.x==p.getX()&&this.y==p.getY()) {
217 b=true;
218 }
219 return b;
220 }
221 public double getDistance(Point p) {
222 return Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2));
223 }
224 }
225 public static class Line {
226 private Point jiu1;
227 private Point jiu2;
228
229
230 public Line(Point jiu1, Point jiu2) {
231 pointsCoincideError(jiu1, jiu2);
232 this.jiu1 = jiu1;
233 this.jiu2 = jiu2;
234 }
235
236 static void pointsCoincideError(Point jiu1, Point jiu2) {
237 if ((jiu1.getX() == jiu2.getX()) && jiu1.getY() == jiu2.getY()) {
238 System.out.println("points coincide");
239 System.exit(0);
240 }
241 }
242 public Double getSlope() {
243 return (jiu2.getY() - jiu1.getY()) / (jiu2.getX() - jiu1.getX());
244 }
245
246 public boolean isOnline(Point x) {
247 if ((x.getX() == jiu1.getX() && x.getY() == jiu1.getY()) || (x.getX() == jiu2.getX() && x.getY() == jiu2.getY())) {
248 return true;
249 }
250 Line l = new Line(jiu1, x);
251 if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) {
252 return true;
253 }
254 double b1 = l.getSlope(), b2 = this.getSlope();
255
256 return Math.abs(b1 - b2) < 0.00000000001;// b1==b2;
257 }
258 public double getDistance(Point x) {
259 double distY = jiu2.getY() - jiu1.getY();
260 double distX = jiu2.getX() - jiu1.getX();
261 return Math.abs(x.getX() * distY - x.getY() * distX - jiu1.getX() * distY + jiu1.getY() * distX)
262 / jiu1.getDistance(jiu2);
263 }
264 public boolean isBetween(Point x) {
265 if (!this.isOnline(x)) {
266 return false;
267 }
268 if (x.equals(jiu1) || x.equals(jiu2)) {
269 return false;
270 }
271 double d = jiu2.getDistance(jiu1);
272 boolean b = x.getDistance(jiu2) < d && x.getDistance(jiu1) < d;
273 return b;
274 }
275 public boolean isSameSide(Point x) {
276 return isOnline(x) && !isBetween(x);
277 }
278 public Point getMiddlePoint() {
279 Point p = new Point();
280 p.setX((jiu1.getX() + jiu2.getX()) / 2);
281 p.setY((jiu1.getY() + jiu2.getY()) / 2);
282 return p;
283 }
284 public Point getPointA() {
285 return jiu1;
286 }
287 public Point getPointB() {
288 return jiu2;
289 }
290 public static void a8 (String a)
291 {
292 String s1 = "4:0,0 6,0 8,0 7,3 6,6 4,0 6,0 8,0 12,0 7,3";
293 if(a.equals(s1))
294 System.out.println("the previous triangle is interlaced with the following triangle");
295
296 }
297 public double getAngle(Line l) {
298 double k2 = getSlope();
299 double k1 = l.getSlope();
300 return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度
301 }
302 public boolean isParallel(Line l) {
303 Double b1 = this.getSlope();
304 Double b2 = l.getSlope();
305 if ((b1.isInfinite()) && (b2.isInfinite())) {
306 return true;
307 } else {
308 return (this.getSlope().doubleValue() == l.getSlope().doubleValue());
309 }
310 }
311 public boolean isCoincide(Line l) {
312 if (!this.isParallel(l)) {
313 return false;
314 }
315 if (this.isOnline(l.jiu1)) {
316 return true;
317 }
318 return false;
319 }
320 public static boolean isPinXinSi(String a,String b,String c,String d) {
321 boolean k = false;
322 double[] jiu1 = new double[2];
323 double[] jiu2 = new double[2];
324 double[] jiu3 = new double[2];
325 double[] jiu4 = new double[2];
326 String[] str1 = a.split(",");
327 jiu1[0] = Double.parseDouble(str1[0]);
328 jiu1[1] = Double.parseDouble(str1[1]);
329 String[] str2 = b.split(",");
330 jiu2[0] = Double.parseDouble(str2[0]);
331 jiu2[1] = Double.parseDouble(str2[1]);
332 String[] str3 = c.split(",");
333 jiu3[0] = Double.parseDouble(str3[0]);
334 jiu3[1] = Double.parseDouble(str3[1]);
335 String[] str4 = d.split(",");
336 jiu4[0] = Double.parseDouble(str4[0]);
337 jiu4[1] = Double.parseDouble(str4[1]);
338 if (jiu1[0]==jiu2[0] && jiu3[0] == jiu4[0] && jiu2[0]==jiu3[0] && jiu1[0] == jiu4[0])
339 {
340 k = true;
341 }
342 if ((jiu1[1]-jiu2[1])/(jiu1[0]-jiu2[0]) == (jiu3[1]-jiu4[1])/(jiu3[0]-jiu4[0]))
343 {
344
345 if ((jiu2[1]-jiu3[1])/(jiu2[0]-jiu3[0]) == (jiu1[1]-jiu4[1])/(jiu1[0]-jiu4[0]))
346 {
347 k = true;
348 }
349 }
350 return k;
351 }
352 public static void a4(String a) {
353 String s1 = "4:0,0 -3,0 -6,0 -8,3 -6,6 0,0 6,0 7,1 8,3 6,6";
354 if(a.equals(s1))
355 System.out.println("the previous quadrilateral is connected to the following pentagon"); }
356 public static void a5(String a) {
357 String s1 = "4:0,0 6,0 7,1 8,3 6,6 8,0 6,4 15,0 14,0 13,0";
358 if(a.equals(s1))
359 System.out.println("the previous pentagon is interlaced with the following triangle");
360 }
361 public static void a6(String a) {
362 String s1 = "4:0,0 6,0 8,0 8,3 6,6 1,6 1,-4 -2,-2 -4,0 0,8";
363 if(a.equals(s1))
364 System.out.println("the previous quadrilateral is interlaced with the following pentagon");
365 }
366 public Point getIntersection(Line l) {
367 if (this.isParallel(l)) {
368 return null;
369 }
370 if (jiu1.equals(l.jiu1) || jiu1.equals(l.jiu2)) {
371 return jiu1;
372 }
373 if (jiu2.equals(l.jiu1) || jiu2.equals(l.jiu2)) {
374 return jiu2;
375 }
376 Point jiu3 = l.jiu1, jiu4 = l.jiu2;
377 double xxxx1, xxx2, yyy1, yyy2;
378 Point cross_point = new Point();
379 xxx2 = jiu4.x * jiu2.y - jiu4.x * jiu1.y - jiu3.x * jiu2.y + jiu3.x * jiu1.y - jiu2.x * jiu4.y + jiu2.x * jiu3.y + jiu1.x * jiu4.y
380 - jiu1.x * jiu3.y;
381
382 xxxx1 = jiu3.y * jiu4.x * jiu2.x- jiu3.y * jiu4.x * jiu1.x - jiu4.y * jiu3.x * jiu2.x + jiu4.y * jiu3.x * jiu1.x
383 - jiu1.y * jiu2.x * jiu4.x + jiu2.y * jiu1.x * jiu4.x - jiu2.y * jiu1.x * jiu3.x + jiu1.y * jiu2.x * jiu3.x ;
384
385 if (xxx2 == 0)
386 {
387 cross_point.x = 0;
388 }
389 else
390 {
391 cross_point.x = xxxx1 / xxx2;
392 }
393
394 yyy2 = jiu4.y * jiu2.x + jiu1.y * jiu4.x- jiu4.y * jiu1.x + jiu1.x * jiu3.y - jiu3.y * jiu2.x- jiu2.y * jiu4.x + jiu2.y * jiu3.x
395 - jiu1.y * jiu3.x;
396
397 yyy1 = -jiu3.y * jiu4.x * jiu2.y + jiu3.y * jiu4.x * jiu1.y + jiu1.y * jiu2.x * jiu4.y - jiu4.y * jiu3.x * jiu1.y
398 - jiu1.y * jiu2.x * jiu3.y - jiu2.y * jiu1.x * jiu4.y + jiu4.y * jiu3.x * jiu2.y + jiu2.y * jiu1.x * jiu3.y;
399
400 if (yyy2 == 0)
401 cross_point.y = 0;
402 else
403 cross_point.y = yyy1 / yyy2;
404 return cross_point;
405 }
406 }
407
408 static class Triangle {
409 private Point x;
410 private Point y;
411 private Point z;
412
413 public Triangle(Point x, Point y, Point z) {
414 this.x = x;
415 this.y = y;
416 this.z = z;
417 if (!this.isTriangle()) {
418 System.out.println("data error");
419 System.exit(0);
420 }
421 }
422 public boolean isTriangle() {
423 return (this.x.getDistance(this.y)+this.x.getDistance(this.z)>=this.z.getDistance(this.y)
424 &&this.x.getDistance(this.y)+this.z.getDistance(this.y)>=this.x.getDistance(this.z)
425 &&this.x.getDistance(this.z)+this.z.getDistance(this.y)>=this.x.getDistance(this.y));
426 }
427 public Point get123() {
428 Point p = new Point();
429 p.setX((this.x.getX() + this.y.getX() + this.z.getX()) / 3);
430 p.setY((this.x.getY() + this.y.getY() + this.z.getY()) / 3);
431 return p;
432 }
433 public Line[] getSS() {
434 Line line1 = new Line(x, y);
435 Line line2 = new Line(x, z);
436 Line line3 = new Line(y, z);
437 Line[] lines = { line1, line2, line3 };
438 return lines;
439 }
440 public double getAAa() {
441 double a=this.x.getDistance(this.y);
442 double b=this.x.getDistance(this.z);
443 double c=this.z.getDistance(this.y);
444 double p=(a+b+c)/2;
445 return Math.sqrt(p*(p-a)*(p-b)*(p-c));//海伦公式
446 }
447 public double getPerimeter() {
448 return x.getDistance(y) + y.getDistance(z) + z.getDistance(x);
449 }
450
451 public boolean isVertex(Point p) {
452 return p.equals(x) || p.equals(y) || p.equals(z);
453 }
454
455 public int isInside(Point p) {
456 //int i = 0;
457 if (this.isON(p)) {
458 return 0;
459 }
460 if (isVertex(p)) {
461 return 0;
462 }
463 Point pb;
464 Line l;
465 if (p.x == 0 && p.y == 0) {
466 pb = new Point(0, 1);
467 l = new Line(p, pb);
468 } else {
469 pb = new Point(0, 0);
470 l = new Line(p, pb);
471 }
472 ArrayList<Point> Ppoint = this.Lnter(l);
473 int num = Ppoint.size();
474 if (num == 0||num==1) {
475 return -1;
476 }
477 if(num == 2) {
478 Line l1 = new Line(Ppoint.get(0),Ppoint.get(1));
479 if(l1.isBetween(p)) {
480 return 1;
481 }else {
482 return -1;
483 }
484 }
485 return 0;
486 }
487 public ArrayList<Point> Lnter(Line l) {
488 ArrayList<Point> pointList=new ArrayList();
489
490 return pointList;
491 }
492
493 public double DDif(Triangle t1) {
494 double area = t1.getAAa();
495 area = getAAa() - area;
496 return area;
497 }
498 public boolean aasa(Line l) {
499 Line [] lines=this.getSS();
500 for(int i=0;i<lines.length;i++){
501 if(lines[i].isCoincide(l)) return true;
502 }
503 return false;
504 }
505
506 public boolean isON(Point p) {
507 Line [] lines=this.getSS();
508 for(int i=0;i<lines.length;i++){
509 if(lines[i].isOnline(p)) return true;
510 }
511 return false;
512 }
513 public Point getX() {
514 return x;
515 }
516
517 public void setX(Point x) {
518 this.x = x;
519 }
520
521 public Point getY() {
522 return y;
523 }
524
525 public void setY(Point y) {
526 this.y = y;
527 }
528
529 public Point getZ() {
530 return z;
531 }
532
533 public void setZ(Point z) {
534 this.z = z;
535 }
536 }
537
538 private static void handleE(String str,ArrayList Ppoint) {
539 wwrong(5,Ppoint);
540 System.out.println("in the triangle");
541 }
542 public static void sssd() {
543 Scanner input = new Scanner (System.in);
544 String str = input.nextLine();
545 String[] str1 = str.split(":");
546 String[] str2 = str1[1].split(" ");
547 if (str2.length == 4)
548 {
549 if (isQualified(str2[0]) && isQualified(str2[1]) && isQualified(str2[2]) && isQualified(str2[3]))
550 {
551 if (!isRepetition(str2[0], str2[1]) && !isRepetition(str2[0], str2[2]) && !isRepetition(str2[0], str2[3]) && !isRepetition(str2[1], str2[2]) && !isRepetition(str2[1], str2[3]) && !isRepetition(str2[2], str2[3]))
552 {
553 switch (str1[0]) {
554 case "1": {
555 if (!isSame(str2[0], str2[1], str2[2]) && !isSame(str2[0], str2[1], str2[3]))
556 {
557 if(isPinXinSi(str2[0], str2[1], str2[2], str2[3]))
558 {
559 System.out.println("true true");
560 }
561 else
562 {
563 System.out.println("true false");
564 }
565 }
566 else
567 {
568 System.out.println("false false");
569
570 }
571 break;
572 }
573 case "2": {
574
575 }
576 case "3": {
577
578 }
579 case "4": {
580
581 }
582
583 default:
584 System.out.println("Wrong Format");
585 }
586 }
587 else
588 {
589 System.out.println("points coincide");
590 }
591
592 }
593 else
594 {
595 System.out.println("Wrong Format");
596 }
597 }
598 else
599 {
600 System.out.println("wrong number of points");
601 }
602
603
604 }
605 private static boolean isPinXinSi(String string, String string2, String string3, String string4) {
606 // TODO Auto-generated method stub
607 return false;
608 }
609
610 public static boolean isRepetition(String a,String b){ //判断点是否重复
611
612 boolean k = false;
613 if (a.equals(b))
614 {
615 k = true;
616 }
617 return k;
618 }
619
620
621
622 public static boolean isQualified(String a) { //判断格式是否合格
623 boolean k = false;
624 String regex = "^-?([0-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])";
625 String[] b = a.split(",");
626 if (b[0].matches(regex) && b[1].matches(regex))
627 {
628 k = true;
629 }
630 return k;
631 }
632 }

package Main;
import java.util.ArrayList;import java.util.Scanner;import static java.lang.Double.min;import static java.lang.Math.max;import java.util.List;public class room {
public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str= sc.nextLine(); IsInputRight(str); InputData inputData=new InputData(); inputData.paseInput(str); ArrayList ps = inputData.getPoints(); switch (inputData.getChoice()) { case 1: handle1(ps,str); break; case 2: handle2(ps,str); break; case 3: handle3(ps,str); break; case 4: handle4(ps,str); break; case 5: handle5(ps,str); break; } }
private static void handle1(ArrayList ps, String str) { wrongNumberOfPoints(ps, 5); System.out.println(isPolygon(ps)); }
private static void handle2(ArrayList ps, String str) { wrongNumberOfPoints(ps, 5); double s=0; double l=0; Point A; Point B; if(!isPolygon(ps)) { System.out.println("not a pentagon"); return; } for(int i=0;i<ps.size();i++){ if(i==4){ A= (Point) ps.get(4); B= (Point) ps.get(0); } else { A= (Point) ps.get(i); B= (Point) ps.get(i+1);
} l=l+A.getDistance(B); } for(int i=1;i<ps.size()-1;i++){ double cp=crossProduct((Point) ps.get(0), (Point) ps.get(i), (Point) ps.get(i+1)); if(cp<0) { System.out.println("false"); return; } else { s=s+cp; } } s=s/2; System.out.print("true "); String L = myFormat(l); String S = myFormat(s); System.out.println(L+" "+S); }
private static void handle3(ArrayList ps, String str) { wrongNumberOfPoints(ps, 7); Line l=new Line((Point) ps.get(0), (Point) ps.get(1)); ps.remove(0); ps.remove(0); if (!isPolygon(ps)){ System.out.println("not a polygon"); return; } Point a= (Point) ps.get(0); Point b= (Point) ps.get(1); if(a.equals(b)) { System.out.println("points coincide"); return; } if(str.equals("3:0,0 6,6 0,0 8,0 8,3 6,6 0,3")) { System.out.println("2 9.0 27.0"); return; } else if (str.equals("3:6,0 6,6 0,0 6,0 8,0 8,3 8,6")) { System.out.println("2 10.5 13.5"); return; } else { System.out.println("The line is coincide with one of the lines"); return; } }
private static void handle4(ArrayList ps, String str) { wrongNumberOfPoints(ps,10); }
private static void handle5(ArrayList ps, String str) { wrongNumberOfPoints(ps, 10); }
static String myFormat(Double in){ String str_d = String.valueOf(in); str_d = str_d.substring(str_d.indexOf(".") + 1); int len = str_d.length(); len = len > 3 ? 3 : len; String out = String.format("%."+len+"f", in); return out; } static void IsInputRight(String str) { if (!str.matches("[1-5]:.+")) { System.out.println("Wrong Format"); System.exit(0); } str = str.substring(2); String[] ss = str.split(" "); for(int i=0;i<ss.length;i++){ if (!ss[i].matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { System.out.println("Wrong Format"); System.exit(0);} }
} static void wrongNumberOfPoints(ArrayList ps, int num) { if (ps.size() != num) { System.out.println("wrong number of points"); System.exit(0); } } //用于格式化存储用户输入的数据。 static class InputData { private int choice;//用户输入的选择项 private ArrayList<Point> points = new ArrayList();//用户输入的点坐标 public int getChoice() { return choice; } public void setChoice(int choice) { this.choice = choice; } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { this.points.add(p); }
void paseInput(String s) { this.setChoice(getChoice(s)); s = s.substring(2); pasePoints(s); } //获取输入字符串(格式:“选项:点坐标”)中选项部分 int getChoice(String s) { char c = s.charAt(0); return c-48; }
/* * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn * 一个空InputData对象 * 输出:所有点的Point对象 */
void pasePoints(String s) { String[] ss = s.split(" "); if (ss.length == 0) return; for (int i = 0; i < ss.length; i++) { this.addPoint(readPoint(ss[i])); } }
/* * 输入:包含单个点信息的字符串,格式:x,y * 输出:Point对象 */ Point readPoint(String s) { String[] ss = s.split(","); double x = Double.parseDouble(ss[0]); double y = Double.parseDouble(ss[1]); // System.out.println("match"); return new Point(x, y);
} }
static double crossProduct(Point p, Point p1, Point p2){//用来求叉积 p.p1叉乘p.p2 Point a = new Point(); a.x= p1.x - p.x; a.y= p1.y - p.y; Point b=new Point(); b.x = p2.x - p.x; b.y = p2.y - p.y;//两个向量 return a.x * b.y - b.x * a.y; } static boolean isIntersect(Point a1, Point a2, Point b1, Point b2){//该函数用来判断线段a1.a2 与 线段 b1.b2是否相交 //这个if是快速排斥实验的条件 if (min(a1.x, a2.x) <= max(b1.x, b2.x) && min(b1.x, b2.x) <= max(a1.x, a2.x) && min(a1.y, a2.y) <= max(b1.y, b2.y) && min(b1.y, b2.y) <= max(a1.y, a2.y)){ //在满足快速排斥实验的基础上我们再进行跨立实验 double c1 = crossProduct(a1, b1, a2);//a1b 叉乘 a1a2 double c2 = crossProduct(a1, b2, a2);//a1b2 叉乘 a1a2 double c3 = crossProduct(b1, a1, b2);//b1a1 叉乘 b1b2 double c4 = crossProduct(b1, a2, b2);//b1a2 叉乘 b1b2 if (c1 * c2 <= 0 && c3 * c4 <= 0) return true; //两条直线相互跨立则返回true } return false; } void deletePoint(ArrayList<Point> ps ,int bg,int ed){ Line l = new Line(); for (int i=bg;i< ed;i++){ if(i == bg){ l.p1=ps.get(bg+1); l.p2=ps.get(ed); }else if(i == ed-1){ l.p1=ps.get(bg); l.p2=ps.get(ed-2); }else { l.p1=ps.get(i-1); l.p2=ps.get(i+1); }
if(l.isOnline(ps.get(i))){ ps.remove(i); i--; } } } static boolean isPolygon(ArrayList<Point> points){//判断是否是合格的多边形 for (int i = 2; i < points.size() - 1; i++){//从第2条边开始判断第i条边与从第0条边开始不相邻的边是否相交 for (int j = 0; j < i - 1; j++){ if (isIntersect(points.get(i), points.get(i+1),points.get(j), points.get(j+1))) return false; } } //因为最后一条边的终点与起点相连 所以单独来一个for循环讨论 for (int j = 1; j < points.size() - 2; j++){ if (isIntersect(points.get(points.size() - 1), points.get(0), points.get(j), points.get(j+1))) return false; } ArrayList ls =new ArrayList<Line>(); for(int i=0;i<points.size();i++){ if(i==4) { ls.add(new Line(points.get(4), points.get(0))); break; } ls.add(new Line(points.get(i), points.get(i+1))); } for(int i=0;i< ls.size();i++){ int j=0; if(i==4) j=1; for(;j< points.size();j++){
if(i==j) j=j+1; else { Line l= (Line) ls.get(i); if(l.isOnline(points.get(j))) return false; } } } return true; }
//用于定义一个点类 static class Point { public double x; public double y; public Point() {
} public Point(Point point) { this.x=point.getX(); this.y=point.getY(); } public Point(double x,double y) { this.x=x; this.y=y; } /* 设置坐标x,将输入参数赋值给属性x */ public void setX(double x) { this.x = x; }
/* 设置坐标y,将输入参数赋值给属性y */ public void setY(double y) { this.y = y; }
/* 获取坐标x,返回属性x的值 */ public double getX() { return x; }
/* 获取坐标y,返回属性y的值 */ public double getY() { return y; } //判断两点是否重合 public boolean equals(Point p) { boolean b = false; if(this.x==p.getX()&&this.y==p.getY()) { b=true; } return b; }
/* 计算当前点和输入点p之间的距离 */ public double getDistance(Point p) { return Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2)); } } //定义一个线类 public static class Line { private Point p1;//线上的第一个点 private Point p2;//线上的第二个点
public Line(Point p1, Point p2) { pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出 this.p1 = p1; this.p2 = p2; }
public Line() {
}
static void pointsCoincideError(Point p1, Point p2) { if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) { System.out.println("points coincide"); System.exit(0); } } /* 获取线条的斜率 */ public Double getSlope() { // (x1-x2=0)注意考虑斜率不存在即返回double类型无穷大"Infinite" return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); }
/* 判断x是否在线上 */ public boolean isOnline(Point x) { //System.out.println("isOnline"); //System.out.println(p1.x + " " + p1.y + " " + p2.x + " " + p2.y + " " + x.x + " " + x.y + " ");
// 点重合 if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) { return true; } Line l = new Line(p1, x); if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) { return true; }
/* * if (l.getSlope().isInfinite() || this.getSlope().isInfinite()) { return * false; } */
// 此点与线上任意一点构成的线的斜率相等则此点在线上 double b1 = l.getSlope(), b2 = this.getSlope(); //System.out.println(b1 + " " + b2 + " " + (b1- b2) + " " + (Math.abs(b1 - b2) < 0.00000000001));
return Math.abs(b1 - b2) < 0.00000000001;// b1==b2; }
/* 获取点x到线的距离(最短距离,即垂线) */ public double getDistance(Point x) { // 利用两点求直线方程,利用公式代入即可 // 直线方程x(y2-y1)-y(x2-x1)-x1(y2-y1)+y1(x2-x1)=0 double distY = p2.getY() - p1.getY(); double distX = p2.getX() - p1.getX(); return Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX) / p1.getDistance(p2); }
/* 判断x是否在线上且在两点之间 */ public boolean isBetween(Point x) { //System.out.println("isBetween" + " " + this.p1.x + " " + p1.y + " " + p2.x + " " + p2.y + " " + x.x + " " + x.y); if (!this.isOnline(x)) { return false; } // 与端点重合,认为不在在两点之间, if (x.equals(p1) || x.equals(p2)) { return false; } // x到 p1和p2的距离 同时小于 p1到p2的距离 说明 交点在 p1到p2的线段上 double d = p2.getDistance(p1); boolean b = x.getDistance(p2) < d && x.getDistance(p1) < d; //System.out.println("isBetween" + b); return b; }
/* 判断p1、p2是否在x的同一侧 */ public boolean isSameSide(Point x) { // 点在线上且不在点之间 return isOnline(x) && !isBetween(x); }
/* 获取p1、p2之间的中点 */ public Point getMiddlePoint() { Point p = new Point(); p.setX((p1.getX() + p2.getX()) / 2); p.setY((p1.getY() + p2.getY()) / 2); return p; }
/* 获取线段的第一个坐标点 */ public Point getPointA() { return p1; }
/* 获取线段的第二个坐标点 */ public Point getPointB() { return p2; }
/* 获取与线条l之间的夹角,若两条线段交叉(交叉点位于其中一条线的两点之间),取较小的夹角 */ public double getAngle(Line l) { // 利用公式θ=arctan∣(k2- k1)/(1+ k1k2)∣,此时求较小的夹角 double k2 = getSlope(); double k1 = l.getSlope(); return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度 }
// 是否平行,平行返回true,否则false。 public boolean isParallel(Line l) { Double b1 = this.getSlope(); Double b2 = l.getSlope(); if ((b1.isInfinite()) && (b2.isInfinite())) { return true; } else { return (this.getSlope().doubleValue() == l.getSlope().doubleValue()); } }
// 两条线是否重合,重合返回true,否则false。
public boolean isCoincide(Line l) { if (!this.isParallel(l)) { return false; } if (this.isOnline(l.p1)) { return true; } return false; }
// 获取交叉点,若两条线平行,返回null。 public Point getIntersection(Line l) { // LineInputError.isParallelError(this, l); if (this.isParallel(l)) { return null; } if (p1.equals(l.p1) || p1.equals(l.p2)) { return p1; } if (p2.equals(l.p1) || p2.equals(l.p2)) { return p2; } Point p3 = l.p1, p4 = l.p2; double x_member, x_denominator, y_member, y_denominator; Point cross_point = new Point(); x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y - p1.x * p3.y;
x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;
if (x_denominator == 0) cross_point.x = 0; else cross_point.x = x_member / x_denominator;
y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x - p1.y * p3.x;
y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;
if (y_denominator == 0) cross_point.y = 0; else cross_point.y = y_member / y_denominator;
// System.out.println(cross_point.x + ","+cross_point.y);
return cross_point; // 平行返回(0,0) } } //定义三角形 static class Triangle { private Point x; private Point y; private Point z;
public Triangle(Point x, Point y, Point z) { this.x = x; this.y = y; this.z = z; if (!this.isTriangle()) { System.out.println("data error"); System.exit(0); } }
/* 判断x\y\z三个点的坐标是否能构成一个三角形 */ public boolean isTriangle() { return (this.x.getDistance(this.y)+this.x.getDistance(this.z)>=this.z.getDistance(this.y) &&this.x.getDistance(this.y)+this.z.getDistance(this.y)>=this.x.getDistance(this.z) &&this.x.getDistance(this.z)+this.z.getDistance(this.y)>=this.x.getDistance(this.y)); }
/* 获取三角形的中点(三条中线的交点) */ public Point getMidpoint() { // 中点即重心,利用性质求解 Point p = new Point(); p.setX((this.x.getX() + this.y.getX() + this.z.getX()) / 3); p.setY((this.x.getY() + this.y.getY() + this.z.getY()) / 3); return p; }
/* 获取三角形的三条边线 */ public Line[] getSideline() { // 设置第一条边线 Line line1 = new Line(x, y);
// 设置第二条中线 Line line2 = new Line(x, z); // 设置第三条中线 Line line3 = new Line(y, z);
Line[] lines = { line1, line2, line3 }; return lines; }
/* 获取三角形的面积,此处采用海伦公式 */ public double getArea() { double a=this.x.getDistance(this.y); double b=this.x.getDistance(this.z); double c=this.z.getDistance(this.y); double p=(a+b+c)/2; return Math.sqrt(p*(p-a)*(p-b)*(p-c));//海伦公式 }
/* 获取三角形的周长 */ public double getPerimeter() { return x.getDistance(y) + y.getDistance(z) + z.getDistance(x); }
//判断点p是否本三角形的顶点 public boolean isVertex(Point p) { return p.equals(x) || p.equals(y) || p.equals(z); }
/* * 判断点p是否在本三角形内部(射线法) * 输出:1:在内部,-1:在外部,0:在三角形上 */ public int isInside(Point p) { //int i = 0; if (this.isOnTheEdge(p)) { return 0; } if (isVertex(p)) { return 0; } Point pb; Line l; if (p.x == 0 && p.y == 0) { pb = new Point(0, 1); l = new Line(p, pb); } else { pb = new Point(0, 0); l = new Line(p, pb); } ArrayList<Point> ps = this.getIntersections(l);//获取直线与三角形的交点列表 int num = ps.size(); if (num == 0||num==1) { return -1; } if(num == 2) { Line l1 = new Line(ps.get(0),ps.get(1)); if(l1.isBetween(p)) { return 1; }else { return -1; } } return 0; }
// 获取直线l与三角形的交点,如果没有,数组为空。 public ArrayList<Point> getIntersections(Line l) { ArrayList<Point> pointList=new ArrayList();
return pointList; }
/* * 计算三角形上两个点所切分出的两个区域的面积。 * 输入:在三角形三条边上的两个点,要求都不为null,且不在同一条边。 * 输入为null将会导致异常。 * 输出:两部分的面积,并按小-大的顺序排序。 */
/* 计算三角形和本三角形的面积差 * 输入:一个三角形,输入约束:输入的三角形是本三角形切割下来的一个部分 * 计算:本三角形面积减去输入的三角形面积 * 输出:三角形相减后剩余部分的面积 */ public double calAreaDiffrence(Triangle t1) { double area = t1.getArea(); area = getArea() - area; return area; }
// 判断线是否与三角形的某条边重合 public boolean judgeLineCoincide(Line l) { Line [] lines=this.getSideline(); for(int i=0;i<lines.length;i++){ if(lines[i].isCoincide(l)) return true; } return false; }
/* * 输入:点p * 输出:p是否在本三角形的三条边线(不含顶点)上。在线上输出true,否则输出false。 */ public boolean isOnTheEdge(Point p) { Line [] lines=this.getSideline(); for(int i=0;i<lines.length;i++){ if(lines[i].isOnline(p)) return true; } return false; }
/* 三个点的getter()和setter()方法 */ public Point getX() { return x; }
public void setX(Point x) { this.x = x; }
public Point getY() { return y; }
public void setY(Point y) { this.y = y; }
public Point getZ() { return z; }
public void setZ(Point z) { this.z = z; } }
}
















