-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
125 lines (112 loc) · 2.77 KB
/
main.pas
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
program Answer;
{$H+}
uses sysutils, classes, math;
// Helper to read a line and split tokens
procedure ParseIn(Inputs: TStrings) ;
var Line : string;
begin
readln(Line);
Inputs.Clear;
Inputs.Delimiter := ' ';
Inputs.DelimitedText := Line;
end;
function ConvertNum(str: String): Extended;
begin
ConvertNum := StrToFloat(StringReplace(str, ',', '.', [])) / 180 * pi;
end;
procedure Split(str: String; out: TStrings);
begin
out.Clear;
out.Delimiter := ';';
out.StrictDelimiter := True;
out.DelimitedText := str;
end;
function Distance(lonA, latA, lonB, latB: Extended): Extended;
var x, y: Extended;
begin
x := (lonB - lonA) * cos((latA + latB) / 2);
y := latB - latA;
Distance := sqrt(x*x + y*y) * 6371;
end;
var
lonA : Extended;
latA : Extended;
lonB : Extended;
latB : Extended;
n : Int32;
defib : String;
i : Int32;
Inputs: TStringList;
tokens: TStringList;
dist: Extended;
minDist: Extended;
ans: String;
begin
Inputs := TStringList.Create;
tokens := TStringList.Create;
ParseIn(Inputs);
lonA := ConvertNum(Inputs[0]);
ParseIn(Inputs);
latA := ConvertNum(Inputs[0]);
ParseIn(Inputs);
n := StrToInt(Inputs[0]);
for i := 0 to N-1 do
begin
readln(defib);
Split(defib, tokens);
lonB := ConvertNum(tokens[4]);
latB := ConvertNum(tokens[5]);
dist := Distance(lonA, latA, lonB, latB);
if ((ans = '') or (dist < minDist)) then
begin
minDist := dist;
ans := tokens[1];
end;
end;
writeln(ans);
flush(StdErr); flush(output); // DO NOT REMOVE
// To debug: writeln(StdErr, 'Debug messages...');
end.
{* Cool solution by someone else
function StrToRad(STR : string) : Real;
begin
DecimalSeparator := ',';
StrToRad := DegToRad(StrToFloat(STR));
end;
function distance(lon1, lat1, lon2, lat2 : Real) : Real;
var
dx, dy : Real;
begin
dx := (lon2 - lon1) * cos(0.5 * (lat1 + lat2));
dy := lat2 - lat1;
distance := sqrt(SQR(dx) + SQR(dy)) * 6371.0;
end;
var
Line : String;
LON1, LON2 : Real;
LAT1, LAT2 : Real;
N : Int32;
i : Int32;
best_name : string;
best_dist, dist : Real;
begin
ReadLn(Line); LON1 := StrToRad(Line);
ReadLn(Line); LAT1 := StrToRad(Line);
ReadLn(N);
best_dist := 1.0 / 0.0;
for i := 1 to N do
begin
readln(Line);
LON2 := StrToRad(ExtractDelimited(5, Line, [';']));
LAT2 := StrToRad(ExtractDelimited(6, Line, [';']));
dist := distance(LON1, LAT1, LON2, LAT2);
if dist < best_dist then
begin
best_name := ExtractDelimited(2, Line, [';']);
best_dist := dist;
end;
end;
WriteLn(best_name);
flush(output);
end.
*}