Friday, December 16, 2016

Kangaroo

There are two kangaroos on an x-axis 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?


Solution :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    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();
        int x1sum=x1;
        int x2sum=x2;
        boolean test=false;
      
       while (x1sum<x2sum || x1sum>=0 && x1sum<=10000  || x2sum>=0 && x2sum<=10000)  {
            x1sum=x1sum+v1;
            x2sum=x2sum+v2;
           
            if(x1sum==x2sum){
                test=true;
                System.out.println("YES");break;
            }
        }
        if(test==false){
            System.out.println("NO");
        }
       
    }
}
 

0 comments:

Post a Comment