C Solve CPS Festival Problem Number 6
Higher category : C Language
a. GitHub
Q. The following four dice are given. Each die contains uppercase alphabets except for Q and V, and no common character appears on any two dice.
It is said that the following words are formed using the above dice:
BOXY, BUCK, CHAW, DIGS, EXAM, FLIT, GIRL, JUMP, OGRE, OKAY, PAWN, ZEST
Find out which die each alphabet belongs to.
Solution.
The following code examines all cases for the remaining alphabets. The values stored in each array represent which die the corresponding alphabet belongs to. Running the code will produce eight results. (Click to check)
1: (×)
2: (O)
3: (×)
4: (×)
5: (×)
6: (×)
7: (×)
8: (×)
In this case, the criteria for determining O and × is solely based on whether each alphabet is divided into six slots. Thus, the problem can be solved without using all the conditions stated in the question. You are advised to separately verify whether Case 2 is the correct answer.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int a[24], i, j;
a[0]=3; // A in the 3rd dice
a[1]=3; // B in the 3rd dice
a[2]=4; // C in the 4th dice
a[4]=1; // E in the 1st dice
a[7]=2; // H in the 2nd dice
a[9]=3; // J in the 3rd dice
a[10]=2; // K in the 2nd dice
a[12]=4; // M in the 4th dice (by EXAM)
a[13]=4; // N in the 4th dice
a[14]=4; // O in the 4th dice
a[15]=2; // P in the 2nd dice
a[19]=1; // U in the 1st dice
a[20]=1; // W in the 1st dice (by CHAW)
a[21]=2; // X in the 2nd dice (by BOXY)
a[22]=1; // Y in the 1st dice
for(a[3]=1; a[3]<5 ;a[3]++){ // D in all dices
for(a[5]=1; a[5]<5; a[5]++){ // F in all dices
for(a[6]=2; a[6]<4; a[6]++){ // G in all dices but 1st dice, 4th dice (cf. OGRE)
for(a[8]=1; a[8]<5; a[8]++){ // I in all dices
for(a[11]=1; a[11]<5; a[11]++){ // L in all dices
for(a[16]=2; a[16]<4; a[16]++){ // R in all dices but 1st dice, 4th dice (cf. OGRE)
for(a[17]=2; a[17]<5; a[17]++){ // S in all dices but 1st dice (cf. ZEST)
for(a[18]=2; a[18]<5; a[18]++){ // T in all dices but 1st dice (cf. ZEST)
for(a[23]=2; a[23]<5; a[23]++){ // Z in all dices but 1st dice (cf. ZEST)
if(a[3]!=a[8] && a[3]!=a[6] && a[3]!=a[17] && a[8]!=a[6] && a[8]!=a[17] && a[6]!=a[17]){
/* condition: DIGS */
if(a[5]!=a[11]
&& a[5]!=a[8] && a[5]!=a[18] && a[11]!=a[8] && a[11]!=a[18] && a[8]!=a[18]){
/* condition: FLIT */
if(a[6]!=a[8] && a[6]!=a[16] && a[6]!=a[11] && a[8]!=a[16] && a[8]!=a[11] && a[16]!=a[11]){
/* condition: GIRL */
if(a[14]!=a[6] && a[14]!=a[16] && a[14]!=a[4] && a[6]!=a[16] && a[6]!=a[4] && a[16]!=a[4]){
/* condition: OGRE */
if(a[23]!=a[4] && a[23]!=a[17] && a[23]!=a[18] && a[4]!=a[17] && a[4]!=a[18] && a[17]!=a[18]){
/* condition: ZEST */
for(j=0; j<24; j++) printf("%d", a[j]); printf("\n");}}}}}}}}}}}}}}
return 0;
}
Input: 2013.09.24 16:22