https://www.codingame.com/training/easy/defibrillators
Practice Loops and Distances with the exercise "Defibrillators"
Want to practice Loops and distances? Try to solve the coding challenge "Defibrillators".
www.codingame.com
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
|
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
lon = input().split(',')
lon = lon[0]+"."+lon[1]
lat = input().split(',')
lat = lat[0]+"."+lat[1]
n = int(input())
defib = ['']*n
for i in range(n):
defib[i] = input().split(';')
defib[i][4] = defib[i][4].split(',')
defib[i][4] = defib[i][4][0]+"."+defib[i][4][1]
defib[i][5] = defib[i][5].split(',')
defib[i][5] = defib[i][5][0]+"."+defib[i][5][1]
minD = float(1000000000000000000000) # 최소거리.
minI = 0 # 최소거리를 가지는 defib의 인덱스.
for i in range(n):
x = (math.radians(float(defib[i][4]))-math.radians(float(lon)))*
math.cos((math.radians(float(lat))+math.radians(float(defib[i][5])))/2)
y = (math.radians(float(defib[i][5]))-math.radians(float(lat)))
d = math.sqrt(x**2 + y**2)*6371
if float(minD) > d:
minD = d
minI = i
# Write an action using print
# To debug: print("Debug messages...", file=sys.stderr)
print(defib[minI][1])
|
cs |
lat = input()
형식으로 입력받으면 43,50982가 스트링 형식으로 입력된다.
43.50982의 실수 형태로 변환해가며 입력받기 위해서는lat = float(input().replace(",", "."))
으로 입력받아야한다.
위의 문제에서 사용하기 위해서는 라디안으로 변환해야하므로lat = math.radians(lat)
으로 변환해준다.
내가 사용했던
lat = input().split(",")
lat = lat[0]+"."+lat[1]
lat = float(lat)
위의 코드에 비하면 아주 간단하게 변환할 수 있다!
+) 파이썬에서 cos 함수와 sqrt함수는 math.cos()
, math.sqrt()
로 사용한다.
'python > codinGame' 카테고리의 다른 글
LUMEN (0) | 2020.03.06 |
---|---|
GRAFFITI ON THE FENCE (0) | 2020.03.06 |
Rectangle Partition (0) | 2020.03.06 |
ANEO Sponsored Puzzle (0) | 2020.03.03 |