Cats and a Mouse - HackerRank || HackerRank Cats and a Mouse Solution

Problem Name: Cats and a Mouse
Judge: HackerRank
Problem link: Cats and a Mouse



HackerRank Cats and a Mouse Solution in C

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int c1, c2, m, n, i, k;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        scanf("%d %d %d", &c1, &c2, &m);
        if (abs(c2 - m) < abs(c1 - m)) printf("Cat B\n");
        else if (abs(c2 - m) > abs(c1 - m)) printf("Cat A\n");
        else printf("Mouse C\n");
    }

    return 0;
}


HackerRank Cats and a Mouse Solution in C++/Cpp

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int c1, c2, m, i, n;
    cin >> n;
    for (i = 1; i <= n; i++)
    {
        cin >> c1 >> c2 >> m;
        if (abs(c2 - m) < abs(c1 - m)) cout << "Cat B" << endl;
        else if (abs(c2 - m) > abs(c1 - m)) cout << "Cat A" << endl;
        else cout << "Mouse C" << endl;
    }

    return 0;
}


HackerRank Cats and a Mouse Solution in Java

import java.util.Scanner;

public class Cats_and_a_Mouse {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int q = input.nextInt();
        int i, x, y, z;
        for (i = 1; i <= q; i++) {
            x = input.nextInt();
            y = input.nextInt();
            z = input.nextInt();
            if (Math.abs(z - x) > Math.abs(z - y)) System.out.println("Cat B");
            else if (Math.abs(z - x) < Math.abs(z - y)) System.out.println("Cat A");
            else System.out.println("Mouse C");
        }
    }
}




Next Post Previous Post
No Comment
Add Comment
comment url