1 package test1;
2
3 import java.util.Scanner;
4 import java.util.TreeMap;
5
6 public class baidu1 {
7
8 /*
9 * 利用实现Comparable接口对每个节点按照 x,y排序,找到最大差值,计算面积
10 * */
11 public static TreeMap<Address,Bit> map = new TreeMap<Address,Bit>();
12 public static int calculateArea(int n, Bit [] mAdds){
13
14 //按照x排序
15 for (int i=0;i<mAdds.length;i++){
16 map.put(new Address(mAdds[i].x),mAdds[i]);
17 }
18 int mX = Math.abs(map.lastEntry().getValue().x - map.firstEntry().getValue().x);
19
20 //按照y排序
21 for (int i=0;i<mAdds.length;i++){
22 map.put(new Address(mAdds[i].y),mAdds[i]);
23 }
24 int mY = Math.abs(map.lastEntry().getValue().y - map.firstEntry().getValue().y);
25
26 if (mX > mY){
27 return mX * mX;
28 } else {
29 return mY * mY;
30 }
31 }
32
33 public static void main(String[] args) {
34
35 System.out.println("请输入节点总数n,以及相应节点的坐标");
36 Scanner scan = new Scanner(System.in);
37 int n = scan.nextInt();
38 int count = 0;
39 Bit[] mBit = new Bit[n];
40
41 while (count < n) {
42
43 int x = scan.nextInt();
44 int y = scan.nextInt();
45 System.out.println("x:" + x + " y:" + y + " n:" + n);
46 Bit mBit1 = new Bit(x, y);
47 mBit[count] = mBit1;
48 count++;
49 }
50 int area = calculateArea(n, mBit);
51
52 System.out.println("正方形的最小面积:"+area);
53 }
54 }
55
56
57 class Address implements Comparable<Object>{
58
59 private int mValue ;
60
61 public Address(int mValue) {
62 super();
63 this.mValue = mValue;
64 }
65
66 @Override
67 public int compareTo(Object o) {
68
69 Address m = (Address)o;
70 if ((this.mValue - m.mValue) == 0){
71 return -1;
72 } else {
73 return (this.mValue - m.mValue);
74 }
75 }
76 }
77
78 class Bit{
79
80 public int x;
81 public int y;
82 public Bit(int x, int y) {
83 super();
84 this.x = x;
85 this.y = y;
86 }
87 }
请输入节点总数n,以及相应节点的坐标
3 0 0 0 3 3 4
x:0 y:0 n:3
x:0 y:3 n:3
x:3 y:4 n:3
正方形的最小面积:16