Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Thursday, June 15, 2017

Cats And Mouse Solution Java


Problem : Two cats named A and B are standing at integral points on the x-axis. Cat A  is standing at point  and cat B is standing at point y . Both cats run at the same speed, and they want to catch a mouse named C that's hiding at integral point  on the x-axis.
Find out who will catch mouse first.




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 q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            int x = in.nextInt();
            int y = in.nextInt();
            int z = in.nextInt();
            int d1=Math.abs(x-z);
            int d2=Math.abs(y-z);
            if(d1==d2)
                System.out.println("Mouse C");
            else if(d1>d2)
                 System.out.println("Cat B");
                else if(d1<d2)
                 System.out.println("Cat A");
        }
    }
}

0 Comments:

Post a Comment

Stats