分别用两个数组去记录每个点的行坐标和列坐标,row[],col[];
对这两个数组排序,然后求数组中最大值和最小值的差,
length = sorted_row[n-1] - sorted_row[0],
width = sorted_col[n-1] - sorted_col[0];
此时得到的长和宽的乘积,是包含所有点的最小矩阵的面积,但是题目要求的是正方形面积,取两个边中的最大值求平方即可。
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int n = scan.nextInt();
int[] col = new int[n];
int[] row = new int[n];
for(int i = 0; i < n; i++){
col[i] = scan.nextInt();
row[i] = scan.nextInt();
}
Arrays.sort(col);
Arrays.sort(row);
int a = col[n-1] - col[0];
int b = row[n-1] - row[0];
int max = Math.max(a*a,b*b);
System.out.println(max);
}
}
}
京公网安备 11010502036488号