Algorithm
백준 2022번 : 사다리
jun_code
2022. 2. 12. 23:16
<문제>
링크 : https://www.acmicpc.net/problem/2022
2022번: 사다리
첫째 줄에 차례대로 x, y, c에 해당하는 양의 실수 세 개가 입력된다. 수는 소수점 여섯째 자리까지 주어질 수 있으며, 3,000,000,000보다 작거나 같다.
www.acmicpc.net
<코드>
import sys
input = sys.stdin.readline
import math
def get_height(x, y, w):
h1 = math.sqrt(x**2-w**2)
h2 = math.sqrt(y**2-w**2)
h = h1*h2/(h1+h2)
return h
x, y, c = map(float, input().split())
w_s = 0
w_e = min(x, y)
dif = 1
res_w = 0
while (w_e - w_s) >= 10**(-6) :
mid = (w_s+w_e)/2
h = get_height(x, y, mid)
if h >= c :
w_s = mid
else:
w_e = mid
res_w = mid
print(res_w)
import sys
input = sys.stdin.readline
import math
def get_height(x, y, w):
h1 = math.sqrt(x**2-w**2)
h2 = math.sqrt(y**2-w**2)
h = h1*h2/(h1+h2)
return h
x, y, c = map(float, input().split())
w_s = 0
w_e = min(x, y)
dif = 1
res_w = 0
while True :
mid = (w_s+w_e)/2
h = get_height(x, y, mid)
dif = abs(c-h)
if dif > 10**(-6):
if c >= h:
w_e = mid
else:
w_s = mid
else:
res_w = mid
break
print(res_w)
<풀이>
- get_height함수를 만들어 특정 너비일 때의 높이를 계산한다.
- 계산한 높이 h와 실제 수치 c를 비교한다.
- h가 c보다 크거나 같은 경우 너비 시작점을 mid로, 그 반대인 경우 너비 끝점을 mid로 설정한다.
- 실제 높이와 계산된 높이를 비교하여 이분 탐색
- ### 해결해야 할 점 : 왜 여기 이분 탐색에서는 +/- 1을 하지 않는가???
참고 : https://github.com/Junuux/Algorithm/tree/main/Silver/2022
GitHub - Junuux/Algorithm
Contribute to Junuux/Algorithm development by creating an account on GitHub.
github.com