题目描述:
在一块地上,有着n(1< =n< =2000) 头牛,输入n,再分别输入这n头牛的坐标(x,y) (1< =x< =100000,1< =y< =100000),如果第i头牛与第j头牛间的距离最近,那么输出i和j
代码:
package lanqiao;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
LinkedHashMap<Integer,String> map = new LinkedHashMap<>();
double min = Double.MAX_VALUE;
int a = 0,b = 0;
String[] c;
double x,y,x1,y1,vue;
for(int i = 1;i <= n;i ++)
{
x = sc.nextDouble();
y = sc.nextDouble();
for(Integer k : map.keySet())
{
c = map.get(k).split(" ");
x1 = Double.parseDouble(c[0]);
y1 = Double.parseDouble(c[1]);
vue = Math.sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y));
if(vue < min)
{
min = vue;
a = k;
b = i;
}
}
map.put(i,x + " " + y);
}
System.out.println(a + " " + b);
}
}