Header Ad

HackerRank Cats and a Mouse problem solution

In this HackerRank Cats and a Mouse problem, 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.

HackerRank Cats and a Mouse problem solution


Problem solution in Python programming.

#!/bin/python3

import sys


q = int(input().strip())
for a0 in range(q):
    x,y,z = input().strip().split(' ')
    x,y,z = [int(x),int(y),int(z)]
    if abs(x-z)>abs(y-z):
        print('Cat B')
    elif abs(x-z)<abs(y-z):
        print('Cat A')
    else:
        print('Mouse C')



Problem solution in Java Programming.

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 catADist = Math.abs(x-z);
            int catBDist = Math.abs(y-z);
            if (catADist == catBDist) {
                System.out.println("Mouse C");
            } else if (catADist < catBDist) {
                System.out.println("Cat A");
            } else {
                System.out.println("Cat B");
            }
            
        }
    }
}


Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int q;
    cin >> q;
    for(int a0 = 0; a0 < q; a0++){
        int x;
        int y;
        int z;
        cin >> x >> y >> z;
        int d1 = abs(x - z), d2 = abs(y - z);
        if (d1 < d2) cout << "Cat A" << endl;
        else if (d1 > d2) cout << "Cat B" << endl;
            else cout << "Mouse C" << endl;
    }
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
typedef unsigned int u32;
typedef          int s32;
int main()
{
	u32 n;scanf("%u",&n);
	for (u32 i=0;i<n;i++)
	{
		s32 x,y,z;scanf("%d %d %d",&x,&y,&z);
		s32 dx=x<z?z-x:x-z;
		s32 dy=y<z?z-y:y-z;
		if (dx<dy) printf("Cat A\n");
		else if (dx>dy) printf("Cat B\n");
		else printf("Mouse C\n");
	}
}


Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var q = parseInt(readLine());
    for(var a0 = 0; a0 < q; a0++){
        var x_temp = readLine().split(' ');
        var x = parseInt(x_temp[0]);
        var y = parseInt(x_temp[1]);
        var z = parseInt(x_temp[2]);
        var distA = Math.abs(x - z);
        var distB = Math.abs(y - z);        

        if( distA === distB){
            console.log("Mouse C");
        }else if(distA > distB){
            console.log("Cat B");
        }else{
            console.log("Cat A");
        }
    }

}


Post a Comment

0 Comments