Pagini recente » Cod sursa (job #2757708) | Cod sursa (job #1836426) | Cod sursa (job #2616108) | Cod sursa (job #1355394) | Cod sursa (job #1545644)
package cmap;
import java.util.*;
import java.io.*;
class Punct {
public int x, y;
Punct(int x, int y) {
this.x = x;
this.y = y;
}
long distanta(Punct B) {
return (B.x - x) * (B.x - x) + (B.y - y) * (B.y - y);
}
}
class PunctXComparator implements Comparator<Punct> {
public int compare(Punct A, Punct B) {
return B.x - A.x;
}
}
class Cmap {
public static void main(String arg[]) {
File f = new File("cmap.in");
if (f.exists())
try {
Scanner sc = new Scanner(f);
int n = sc.nextInt();
Punct[] puncte = new Punct[n];
for (int i = 0; i < n; i++)
puncte[i] = new Punct(sc.nextInt(), sc.nextInt());
Arrays.sort(puncte, new PunctXComparator());
double solutie = closest(puncte);
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("cmap.out")))) {
writer.write(Double.toString(solutie));
} catch (IOException e) {
e.printStackTrace();
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
else
System.out.println("File not found!");
}
public static double closest(Punct[] puncte) {
long solution = Long.MAX_VALUE;
int end=puncte.length;
int frame = 4;
for (int i = 0; i < end-1; ++i) {
for (int j = i + 1, end2 = (i + frame > end ? end : i + frame); j < end2; ++j) {
if (solution > puncte[i].distanta(puncte[j])) {
solution = puncte[i].distanta(puncte[j]);
}
}
}
return Math.sqrt(solution);
}
}