728x90
반응형
https://www.acmicpc.net/problem/2869
2869번: 달팽이는 올라가고 싶다
첫째 줄에 세 정수 A, B, V가 공백으로 구분되어서 주어진다. (1 ≤ B < A ≤ V ≤ 1,000,000,000)
www.acmicpc.net
문제
입력
출력
예제 입력 1
2 1 5
예제 출력 1
4
예제 입력 2
5 1 6
예제 출력 2
2
예제 입력 3
100 99 1000000000
예제 출력 3
999999901
풀이
설명
- 수정 전 코드는 시간이 너무 오래 걸려서 정상 동작하지 않는다.
- 수정 후 코드에서 (V-B)/(A-B) 수식은 정상에 한번 도달하면 밤에 미끄러지지 않는 것을 고려해 준 것이다.
- 마지막에 정수형으로 출력하기 위해 math 라이브러리 내에 있는 ceil()을 사용하여 올림한다.
코드
- 수정 전 코드
import sys
input = sys.stdin.readline
A, B, V = map(int, input().split())
cnt = 0
location = 0
while True:
if location == V:
break
location += A
location -= B
cnt += 1
print(cnt-1)
- 수정 후 코드
import sys
import math
input = sys.stdin.readline
A, B, V = map(int, input().split())
cnt = (V-B)/(A-B)
print(math.ceil(cnt))
728x90
반응형
'Algorithm > Baekjoon' 카테고리의 다른 글
[Algorithm] 백준 5086번 배수와 약수 (Python) (0) | 2023.04.01 |
---|---|
[Algorithm] 백준 4949번 균형잡힌 세상 (Python) (0) | 2023.03.31 |
[Algorithm] 백준 1436번 영화감독 숌 (Python) (0) | 2023.03.29 |
[Algorithm] 백준 10773번 제로 (Python) (0) | 2023.03.28 |
[Algorithm] 백준 1018번 체스판 다시 칠하기 (Python) (0) | 2023.03.27 |