正方形的判定:对角线长度相等,且互相垂直平分

魔法阵

http://www.nowcoder.com/questionTerminal/04c873db99a34a0c85d476be82533b98

这题的x,y坐标分量仅限于个位数,读取的时候把它们拆开就行。
正方形的判定:对角线长度相等,且互相垂直平分。

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int T = Integer.parseInt(sc.nextLine());
        for(int j = 0; j < T; ++j){
            String X = sc.nextLine(), Y = sc.nextLine();
            int[] x = new int[4], y = new int[4];
            for(int i = 0; i < 4; ++i){
                x[i] = X.charAt(i) - '0';
                y[i] = Y.charAt(i) - '0';
            }
            if(f(x, y)) System.out.println("Yes");
            else System.out.println("No");
        }
    }
    static boolean f(int[] x, int[] y){
        for(int i = 0; i < 3; ++i){
            int a = (i + 1)%3, b = (i + 2)%3;    //除了3与i的另外两个下标
            if(x[3] + x[i] == x[a] + x[b] && y[3] + y[i] == y[a] + y[b]){    //对角线互相平分
                if((x[3] - x[i])*(x[a] - x[b]) + (y[3] - y[i])*(y[a] - y[b]) == 0){    //对角线互相垂直
                    int t1 = (x[3] - x[i])*(x[3] - x[i]) + (y[3] - y[i])*(y[3] - y[i]);
                    int t2 = (x[a] - x[b])*(x[a] - x[b]) + (y[a] - y[b])*(y[a] - y[b]);
                    if(t1 == t2){    //对角线长度相等
                        return true;
                    }
                }
            }
        }
        return  false;
    }
}
全部评论

相关推荐

二十岁的编程男神王大...:读博吧兄弟,你这绩点太好了,何必转码,另外哈哈哈真见到有括号标出来985的,这个不标注也知道吧
点赞 评论 收藏
分享
想做乐观锁:都不用AI,咱们都古法编程吧,让节奏慢一点
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务