Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your task is to determine which cat will reach the mouse first, assuming the mouse does not move and the cats travel at equal speed. If the cats arrive at the same time, the mouse will be allowed to move and it will escape while they fight.
You are given q queries in the form of x,y, and z representing the respective positions for cats A and B, and for mouse C.
1.If cat A catches the mouse first, print Cat A
.
2.If the cat B catches the mouse first, print Cat B
.
3.If both cats reach the mouse at the same time, print Mouse C
as the two cats fight and the mouse escapes.
Example:
Input: q=1, x=1,y=3,z=2
Output: Mouse C
Approach
C++
#include <bits/stdc++.h>using namespace std;string catAndMouse(int x, int y, int z){if (abs(x - z) == abs(y - z))return "Mouse C";else if (abs(x - z) > abs(y - z))return "Cat B";return "Cat A";}int main(){int q = 1;int x = 1, y = 3, z = 2;cout << catAndMouse(x, y, z);return 0;}
No comments:
Post a Comment