#1040 : 矩形判断

来源:互联网 发布:mac上windows虚拟机 编辑:程序博客网 时间:2024/05/22 14:35

我觉得测试用例可能很少很easy


import java.awt.Point;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner scanner = new Scanner(System.in);int T = scanner.nextInt();double[] slope = new double[4];while (T-- != 0) {for (int i = 0; i < 4; i++) {int x1 = scanner.nextInt();int y1 = scanner.nextInt();int x2 = scanner.nextInt();int y2 = scanner.nextInt();if (x1 == x2) {slope[i] = Integer.MAX_VALUE;}else {slope[i] = (y1 - y2)/(x1 - x2 + 0.0);}}if (isRectangle(slope)) {System.out.println("YES");}else {System.out.println("NO");}}}private static boolean isEquals(double a, double b){if (a - b >= -0.0000000001 && a - b <= 0.0000000001) {return true;}else {return false;}}private static boolean isRectangle(double[] slope) {// TODO Auto-generated method stubdouble s1 = slope[0], s2 = slope[0];int count = 0;for (int i = 1; i < slope.length; i++) {if (!isEquals(slope[i], s1) && !isEquals(slope[i], s2)) {count++;s2 = slope[i];}}if (count == 0 || count > 1) {return false;}if (s1 > s2) {s1 = s1 + s2;s2 = s1 - s2;s1 = s1 - s2;}if (isEquals(s1, 0) && isEquals(s2, Integer.MAX_VALUE)) {return true;}else {if (isEquals((s1 * s2 + 1), 0)) {return true;}else {return false;}}}}


0 0
原创粉丝点击