forked from RiviereGregory/CodinGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Defibrillators.txt
50 lines (39 loc) · 1.78 KB
/
Defibrillators.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.*;
import java.io.*;
import java.math.*;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double longitudePos = Double.valueOf(in.nextLine().replaceAll(",","\\."));
double latitudePos = Double.valueOf(in.nextLine().replaceAll(",","\\."));
double longitudePosRad = longitudePos*Math.PI/180;
double latitudePosRad = latitudePos*Math.PI/180;
int n = Integer.parseInt(in.nextLine());
String nomPositionDefib = "";
double distanceMin = 100000;
for (int i = 0; i < n; i++) {
String line = in.nextLine();
String[] lineSplit = line.split(";");
double longitudePosDefib = Double.valueOf(lineSplit[4].replaceAll(",","\\."));
double latitudePosDefib = Double.valueOf(lineSplit[5].replaceAll(",","\\."));
double longitudePosDefibRad = longitudePosDefib*Math.PI/180;
double latitudePosDefibRad = latitudePosDefib*Math.PI/180;
double distanceDefib = distance(longitudePosRad,longitudePosDefibRad,latitudePosRad,latitudePosDefibRad);
if(distanceDefib < distanceMin){
distanceMin = distanceDefib;
nomPositionDefib = lineSplit[1];
}
}
System.out.println(nomPositionDefib);
}
public static double X(double longitudeA,double longitudeB,double latitudeA,double latitudeB){
return (longitudeB-longitudeA)*Math.cos((latitudeA+latitudeB)/2);
}
public static double Y(double latitudeA,double latitudeB){
return (latitudeB-latitudeA);
}
public static double distance(double longitudeA,double longitudeB,double latitudeA,double latitudeB){
return (Math.sqrt(X(longitudeA,longitudeB,latitudeA,latitudeB)*X(longitudeA,longitudeB,latitudeA,latitudeB)
+ Y(latitudeA,latitudeB)*Y(latitudeA,latitudeB)) * 6371);
}
}