Last active
August 29, 2015 16:01
-
-
Save micahstairs/fefdc9a3303f0dd64db8 to your computer and use it in GitHub Desktop.
Solution for Logic 1 (https://youtu.be/PQIR2E-xva8)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class MagicTrick { | |
static Scanner sc = new Scanner(System.in); | |
public static void main(String[] args) { | |
int t = Integer.valueOf(sc.nextLine()); | |
for (int i = 1; i <= t; i++) { | |
// Handling input | |
int guess1 = Integer.valueOf(sc.nextLine()); | |
int[][] cards1 = readCards(); | |
int guess2 = Integer.valueOf(sc.nextLine()); | |
int[][] cards2 = readCards(); | |
// Find common cards | |
int[] pickedCards1 = cards1[guess1 - 1]; | |
int[] pickedCards2 = cards2[guess2 - 1]; | |
int nCommonCards = 0; | |
int commonCard = -1; | |
for (int card1 : pickedCards1) { | |
for (int card2 : pickedCards2) { | |
if (card1 == card2) { | |
nCommonCards++; | |
commonCard = card1; | |
} | |
} | |
} | |
// Print the result | |
String result; | |
if (nCommonCards == 0) | |
result = "Volunteer cheated!"; | |
else if (nCommonCards == 1) | |
result = String.valueOf(commonCard); | |
else | |
result = "Bad magician!"; | |
System.out.printf("Case #%d: %s\n", i, result); | |
} | |
} | |
public static int[][] readCards() { | |
int[][] cards = new int[4][4]; | |
for (int row = 0; row < 4; row++) { | |
String[] line = sc.nextLine().split(" "); | |
for (int col = 0; col < 4; col++) { | |
cards[row][col] = Integer.valueOf(line[col]); | |
} | |
} | |
return cards; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment