코드비교

Old VersionNew Version
1import java.util.Arrays; 1import java.util.Arrays; 
2import java.util.Scanner; 2import java.util.Scanner; 
3import java.lang.String;   
4public class Main { 3public class Main { 
5public static void main(String[] args) { 4public static void main(String[] args) { 
6Scanner sc = new Scanner(System.in); 6Scanner in = new Scanner(System.in);
7int rep = sc.nextInt(); 7int T = in.nextInt();
8for (int reps = 0; reps < rep; reps++) {//이 안에서 실행하자 8int[][] arr1 = new int[4][2];
9int[][] pos = new int[4][2];//앞에것은 순서, 뒤에 두칸은 x,y 9for (int n = 0; n < T; n++) {
10for (int i = 0; i < 4; i++) { 9for (int i = 0; i < 4; i++) { 
11for (int j = 0; j < 2; j++) { 11arr1[i][0] = in.nextInt();
12pos[i][j] = sc.nextInt(); 12arr1[i][1] = in.nextInt();
13} 12} 
14}//입력받기 완료 14String result = issqr(getlengths(arr1)) ? "square" : "not square";
15if (checkLength(pos) == false) { 15System.out.println(result);
16System.out.printf("not square\n");   
17continue;   
18}   
19System.out.printf("square\n");   
20} 15} 
21} 16} 
22public static boolean checkLength(int pos[][]) { 22public static boolean issqr(double lengths[]) {
23double[] len = new double[6]; 23if (lengths[5] == 0) return false;
24len[0] = length(pos, 0,1); 24if (lengths[0] != lengths[3] || lengths[4] != lengths[5]) return false;
25len[1] = length(pos, 0,2); 25return 2 * lengths[0] == lengths[4];
26len[2] = length(pos, 0,3);   
27len[3] = length(pos, 1,2);   
28len[4] = length(pos, 1,3);   
29len[5] = length(pos, 2,3);   
30Arrays.sort(len);   
31if(len[0]==len[1] && len[0]==len[2] && len[0]==len[3] && len[4]==len[5] && len[4]!=len[0])   
32return true;   
33return false;   
34} 21} 
35public static double length(int pos[][], int n1, int n2) { 22public static double[] getlengths(int[][] arr) {
36return Math.sqrt(Math.pow(((double)pos[n1][0]-pos[n2][0]),2)+Math.pow(((double)pos[n1][1]-pos[n2][1]),2)); 23double[] lengths1 = new double[6];
   24lengths1[0] = sqdist(arr[0], arr[1]);
   25lengths1[1] = sqdist(arr[0], arr[2]);
   26lengths1[2] = sqdist(arr[0], arr[3]);
   27lengths1[3] = sqdist(arr[1], arr[2]);
   28lengths1[4] = sqdist(arr[1], arr[3]);
   29lengths1[5] = sqdist(arr[2], arr[3]);
   30Arrays.sort(lengths1);
   31return lengths1;
   32}
   33public static double sqdist(int[] p, int[] q) {
   34return Math.pow(p[0] - q[0], 2) + Math.pow(p[1] - q[1], 2);
37} 35} 
38} 36} 
import java.util.Arrays;
import java.util.Scanner;
import java.lang.String;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rep = sc.nextInt();
for (int reps = 0; reps < rep; reps++) {//이 안에서 실행하자
int[][] pos = new int[4][2];//앞에것은 순서, 뒤에 두칸은 x,y
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
pos[i][j] = sc.nextInt();
}
}//입력받기 완료
if (checkLength(pos) == false) {
System.out.printf("not square\n");
continue;
}
System.out.printf("square\n");
}
}
public static boolean checkLength(int pos[][]) {
double[] len = new double[6];
len[0] = length(pos, 0,1);
len[1] = length(pos, 0,2);
len[2] = length(pos, 0,3);
len[3] = length(pos, 1,2);
len[4] = length(pos, 1,3);
len[5] = length(pos, 2,3);
Arrays.sort(len);
if(len[0]==len[1] && len[0]==len[2] && len[0]==len[3] && len[4]==len[5] && len[4]!=len[0])
return true;
return false;
}
public static double length(int pos[][], int n1, int n2) {
return Math.sqrt(Math.pow(((double)pos[n1][0]-pos[n2][0]),2)+Math.pow(((double)pos[n1][1]-pos[n2][1]),2));
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
int[][] arr1 = new int[4][2];
for (int n = 0; n < T; n++) {
for (int i = 0; i < 4; i++) {
arr1[i][0] = in.nextInt();
arr1[i][1] = in.nextInt();
}
String result = issqr(getlengths(arr1)) ? "square" : "not square";
System.out.println(result);
}
}
public static boolean issqr(double lengths[]) {
if (lengths[5] == 0) return false;
if (lengths[0] != lengths[3] || lengths[4] != lengths[5]) return false;
return 2 * lengths[0] == lengths[4];
}
public static double[] getlengths(int[][] arr) {
double[] lengths1 = new double[6];
lengths1[0] = sqdist(arr[0], arr[1]);
lengths1[1] = sqdist(arr[0], arr[2]);
lengths1[2] = sqdist(arr[0], arr[3]);
lengths1[3] = sqdist(arr[1], arr[2]);
lengths1[4] = sqdist(arr[1], arr[3]);
lengths1[5] = sqdist(arr[2], arr[3]);
Arrays.sort(lengths1);
return lengths1;
}
public static double sqdist(int[] p, int[] q) {
return Math.pow(p[0] - q[0], 2) + Math.pow(p[1] - q[1], 2);
}
}