TOPIC

Java HashMap solution, 5% WA

Saiful Islam asked 2 months ago

import java.io.IOException;
import java.util.*;

public class Main {

public static void main(String[] args) throws IOException {

HashMap<String, String[]> map = new HashMap<>();
map.put("pedra", new String[]{"lagarto", "tesoura"});
map.put("papel", new String[]{"spock", "pedra"});
map.put("tesoura", new String[]{"lagarto", "papel"});
map.put("lagarto", new String[]{"papel", "spock"});
map.put("spock", new String[]{"pedra", "lagarto"});

Scanner sc = new Scanner(System.in);

int t = sc.nextInt();
int n = 0;
while(n < t) {
int i = 0;
String[] words = new String[2];
while(i<2) {
String line = sc.next();
words[i] = line;
i++;
}
System.out.println("Caso #"+ (n+1) + ": " + test(map, words));
n++;
}

}

public static String test(HashMap<String, String[]> map, String[] words) {
String a = words[0].toLowerCase();
String b = words[1].toLowerCase();

if(map.containsKey(a) && map.containsKey(b)) {
String[] values = map.get(a);
if(values[0].equals((b)) || values[1].equals(b)) {
return "Bazinga!";
}

values = map.get(b);
if(values[0].equals((a)) || values[1].equals(a)) {
return "Raj trapaceou!";
}
}
return "De novo!";
}

}

Remember not post solutions. Your post may be reviewed by our moderators.

  • josean replied 2 months ago

    test it:

    3
    tesoura Spock
    Spock tesoura
    Spock lagarto

    output correct:

    Caso #1: Raj trapaceou!
    Caso #2: Bazinga!
    Caso·#3: Raj trapaceou!

    output your code:

    Caso #1: De novo!
    Caso #2: De novo!
    Caso #3: Bazinga!