Kangaroo     

                           

There are two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location  and moves at a rate of  meters per jump. The second kangaroo starts at location  and moves at a rate of  meters per jump. Given the starting locations and movement rates for each kangaroo, can you determine if they'll ever land at the same location at the same time?

Input Format

A single line of four space-separated integers denoting the respective values of , and .

Constraints

Output Format

Print YES if they can land on the same location at the same time; otherwise, print NO.

Note: The two kangaroos must land at the same location after making the same number of jumps.

Sample Input 0

0 3 4 2

Sample Output 0

YES

Explanation 0

The two kangaroos jump through the following sequence of locations:

Thus, the kangaroos meet after  jumps and we print YES.

Sample Input 1

0 2 5 3

Sample Output 1

NO

Explanation 1

The second kangaroo has a starting location that is ahead (further to the right) of the first kangaroo's starting location (i.e., ). Because the second kangaroo moves at a faster rate (meaning and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO.

 

 



풀이 과정

"양의 x좌표 상에 두 시작점이 양의 방향으로 각각 일정한 값 만큼 움직일 때 두 점이 만나는 시점의 유무 찾기" 

방정식 해를 찾으려 괜한 반복문을 범위 설정도 명확히 하지 못한 채 돌렸다. 막연히 회전수를 키워서 답은 통과했지만 바람직한 해결책은 아니다.


풀이 비교 

* v1, v2 비교를 정수 값 체크와 한꺼번에 할 수 있었다.

* 만나는 시점 체크도 정확히 몇 번째에 만나는 가를 묻는 것이 아니기 때문에 방정식을 정리하여 정수 유무(나머지 0)만 체크해도 됬었다.

이렇게 간단한 것을!!! 



  

  public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int x1 = in.nextInt();

        int v1 = in.nextInt();

        int x2 = in.nextInt();

        int v2 = in.nextInt();

        

        if ( v1>v2 && (x2-x1)%(v1-v2)==0 )

            System.out.println("YES");

        else

            System.out.println("NO");

    }




+ Recent posts