Skip to content

Google Code Jam. Magic Trick

The Magic Trick is the Problem A of Google Code Jam 2014 Qualification Round.

Here is my solution for it in C language:


#include <stdio.h>

main() {
    int i, j, T, first, second,
	F[4], S[4], t[4], r, n;

    scanf("%d\n", &T);
    for (i = 0; i < T; i++) {
	printf("Case #%d: ", i + 1);
	scanf("%d", &first);
	for(j = 0; j < 4; j++) {
	    if (j == first - 1) {
		scanf("%d %d %d %d", F, F+1, F+2, F+3);
	    } else {
		scanf("%d %d %d %d", t, t+1, t+2, t+3); 
	    }
	}
	scanf("%d", &second);
	for(j = 0; j < 4; j++) {
	    if (j == second - 1) {
		scanf("%d %d %d %d", S, S+1, S+2, S+3);
	    } else {
		scanf("%d %d %d %d", t, t+1, t+2, t+3); 
	    }
	}
	n = 0;
	for(j = 0; j < 4; j++) {
	    if (F[j] == S[0] || F[j] == S[1] || F[j] == S[2] || F[j] == S[3]) {
		n++;
		r = F[j];
	    }
	}
	switch(n) {
	case 0: printf("Volunteer cheated!\n"); break;
	case 1: printf("%d\n", r); break;
	default:printf("Bad magician!\n");
	}
    }
}

Leave a Reply

Your email address will not be published.