-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathJava.java
64 lines (52 loc) · 2.01 KB
/
Java.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/****************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: Defibrillators */
/* Difficulty: Easy */
/* Date solved: 08.11.2018 */
/* */
/****************************************/
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//Read inputs.
Scanner scanner = new Scanner(System.in);
double lonA = Double.parseDouble(scanner.nextLine().replace(",", "."));
double latA = Double.parseDouble(scanner.nextLine().replace(",", "."));
int N = Integer.parseInt(scanner.nextLine());
//Define minimum.
double min = Double.MAX_VALUE;
String minName = "";
for (int i = 0; i < N; i++)
{
//Read defibrillator.
String[] DEFIB = scanner.nextLine().split(";");
double lonB = Double.parseDouble(DEFIB[4].replace(",", "."));
double latB = Double.parseDouble(DEFIB[5].replace(",", "."));
//Calculating distance for current defibrillator.
double d = Distance(lonA, latA, lonB, latB);
//Set nearest defibrillator.
if (d < min)
{
min = d;
minName = DEFIB[1];
}
}
//Print nearest defibrillator.
System.out.println(minName);
}
//Calculates the distance between two points on earth.
private static double Distance(double lonA, double latA, double lonB, double latB)
{
lonA = Math.toRadians(lonA);
lonB = Math.toRadians(lonB);
latA = Math.toRadians(latA);
latB = Math.toRadians(latB);
double x = (lonB - lonA) * Math.cos((latA + latB) / 2);
double y = latB - latA;
return Math.sqrt(x * x + y * y) * 6371;
}
}