这题的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; } }