2019년 4월 13일 토요일

# FrogJmp 코딜리티 레슨 문제

코딜리티 lesson 문제.
문제는 아래와 같다.
A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D.
Count the minimal number of jumps that the small frog must perform to reach its target.
개구리가 X라는 위치에서 점프를 해서 Y보다 같거나 큰값을 가지고 싶어하는거다. 그리고 개구리는 D만큼 고정된 값으로
점프를 한다. 
우선 간단하게 식을 만들었다. Y = X + (점프횟수 * D) 을 만들고 점프횟수로 정리를 하면 
점프횟수 = ( Y - X ) / D 이고, 이는 최소한으로 이정도는 뛰어야 값에 똑같다라는걸 의미한다. 그러나 개구리가 열심히 점프를 해서
Y값에 똑같거나 또는 커야 하므로 이 식을 통해 구하는 값보다 클 수 있다. 따라서 코드는 아래처럼 구성했다.
1
2
3
4
5
6
int solution(int X, int Y, int D){
 int cnt = 0;
 cnt = (Y - X) / D;
 if((X + cnt * D) >= Y) return cnt;
 else return ++cnt;
}

댓글 없음:

댓글 쓰기