爱因斯坦谜题的c语言解答
[旧文搬迁]
题目如下,
1、在一条街上,有5座房子,喷了5种颜色。 2、每个房里住着不同国籍的人 3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物 另外, 1、英国人住红色房子 2、瑞典人养狗 3、丹麦人喝茶 4、绿色房子在白色房子左面 5、绿色房子主人喝咖啡 6、抽Pall Mall香烟的人养鸟 7、黄色房子主人抽Dunhill香烟 8、住在中间房子的人喝牛奶 9、挪威人住第一间房 10、抽Blends香烟的人住在养猫的人隔壁 11、养马的人住抽Dunhill香烟的人隔壁 12、抽Blue Master的人喝啤酒 13、德国人抽Prince香烟 14、挪威人住蓝色房子隔壁 15、抽Blends香烟的人有一个喝水的邻居
问题是:谁养鱼?
以下是c语言的解答
#include <stdio.h> #include <stdlib.h> enum Nat { ENG,SWE,DEN,NOR,GER }; enum Hou { RED,GREEN,WHITE,YELLOW,BLUE }; enum Pet { DOG,BIRD,CAT,HORSE,FISH }; enum Dri { TEA,COFFEE,MILK,BEER,WATER }; enum Cig { PALLMALL,DUNHILL,BLENDS,BLUEMASTER,PRINCE }; struct S//scence { int *nat; //nationality int *hou; //house int *pet; //pet int *dri; //drink int *cig; //cigar } ; void Permute(int a[120][5]) //permutation process { int i=0,j=0,r1,r2,r3,r4,r5; for (r1=0;r1<5;r1++) { for (r2=0;r2<5;r2++) { if (r2!=r1) for (r3=0;r3<5;r3++) { if (r3!=r2&&r3!=r1) for (r4=0;r4<5;r4++) { if (r4!=r3&&r4!=r2&&r4!=r1) for (r5=0;r5<5;r5++) { if (r5!=r4&&r5!=r3&&r5!=r2&&r5!=r1) { a[i][j++]=r1; a[i][j++]=r2; a[i][j++]=r3; a[i][j++]=r4; a[i++][j]=r5; j=0; } } } } } } //printf("\n***枚举过程完成***\n\n"); return ; } int NatPos(struct S s,enum Nat t) //get position by nationality { int i; for (i=0;i<5;i++) if (s.nat[i]==t) return i; return -1; } int HouPos(struct S s,enum Hou t) //get position by house { int i; for (i=0;i<5;i++) if (s.hou[i]==t) return i; return -2; } int PetPos(struct S s,enum Pet t) //get position by pet { int i; for (i=0;i<5;i++) if (s.pet[i]==t) return i; return -3; } int DriPos(struct S s,enum Dri t) //get position by drink { int i; for (i=0;i<5;i++) if (s.dri[i]==t) return i; return -4; } int CigPos(struct S s,enum Cig t) //get position by cigar { int i; for (i=0;i<5;i++) if (s.cig[i]==t) return i; return -5; } int main() { int a[120][5]; struct S s; int r1,r2,r3,r4,r5,i; Permute(a); //get permuted array for (s.dri=a[0],r1=0;r1<120;s.dri+=5,r1+=1) //permute 5 rounds for scene { if (s.dri[2]==MILK) //condition required for (s.nat=a[0],r2=0;r2<120;s.nat+=5,r2+=1) { if (s.nat[0]==NOR&&s.dri[NatPos(s,DEN)]==TEA) //condition required for (s.hou=a[0],r3=0;r3<120;s.hou+=5,r3+=1) { if (s.hou[NatPos(s,ENG)]==RED&&s.dri[HouPos(s,GREEN)]==COFFEE&& (HouPos(s,GREEN)+1==HouPos(s,WHITE))&& ((NatPos(s,NOR)==HouPos(s,BLUE)+1)||(NatPos(s,NOR)==HouPos(s,BLUE)-1))) for (s.pet=a[0],r4=0;r4<120;s.pet+=5,r4+=1) { if (s.pet[NatPos(s,SWE)]==DOG) //condition required for (s.cig=a[0],r5=0;r5<120;s.cig+=5,r5+=1) { //<---- ////////////////////////////////////////////////////////////////// if (((CigPos(s,BLENDS)==DriPos(s,WATER)+1)||(CigPos(s,BLENDS)==DriPos(s,WATER)-1))&& ((PetPos(s,HORSE)==CigPos(s,DUNHILL)+1)||(PetPos(s,HORSE)==CigPos(s,DUNHILL)-1))&& ((CigPos(s,BLENDS)==PetPos(s,CAT)+1)||(CigPos(s,BLENDS)==PetPos(s,CAT)-1))&& s.dri[CigPos(s,BLUEMASTER)]==BEER&&s.pet[CigPos(s,PALLMALL)]==BIRD&& s.cig[HouPos(s,YELLOW)]==DUNHILL&&s.cig[NatPos(s,GER)]==PRINCE) { switch (s.nat[PetPos(s,FISH)]) { case ENG:printf("***英国人养鱼***\n");break; case SWE:printf("***瑞典人养鱼***\n");break; case DEN:printf("***丹麦人养鱼***\n");break; case NOR:printf("***挪威人养鱼***\n");break; case GER:printf("***德国人养鱼***\n");break; default:printf("***解不存在***\n");break; } printf("\n此时具体情况如下"); printf("\n\n*****************\n"); printf(" 标号 1 2 3 4 5 \n"); printf("\n 国籍 "); for (i=0;i<5;i++) { switch (s.nat[i]) { case ENG:printf("英国 ");break; case SWE:printf("瑞典 ");break; case DEN:printf("丹麦 ");break; case NOR:printf("挪威 ");break; case GER:printf("德国 ");break; default:printf("错误 ");break; }; } printf("\n\n 房屋颜色 "); for (i=0;i<5;i++) { switch (s.hou[i]) { case RED:printf("红色 ");break; case GREEN:printf("绿色 ");break; case WHITE:printf("白色 ");break; case YELLOW:printf("黄色 ");break; case BLUE:printf("蓝色 ");break; default:printf("错误 ");break; }; } printf("\n\n 宠物 "); for (i=0;i<5;i++) { switch (s.pet[i]) { case DOG:printf("狗 ");break; case BIRD:printf("鸟 ");break; case CAT:printf("猫 ");break; case HORSE:printf("马 ");break; case FISH:printf("鱼 ");break; default:printf("错误 ");break; }; } printf("\n\n 饮品 "); for (i=0;i<5;i++) { switch (s.dri[i]) { case TEA:printf("茶 ");break; case COFFEE:printf("咖啡 ");break; case MILK:printf("牛奶 ");break; case BEER:printf("啤酒 ");break; case WATER:printf("水 ");break; default:printf("错误 ");break; }; } printf("\n\n 香烟 "); for (i=0;i<5;i++) { switch (s.cig[i]) { case PALLMALL:printf("帕玛 ");break; case DUNHILL:printf("杜希尔 ");break; case BLENDS:printf("布莱登 ");break; case BLUEMASTER:printf("布鲁玛 ");break; case PRINCE:printf("布里斯 ");break; default:printf("错误 ");break; }; } printf("\n\n*****************\n\n"); } } } } } } return 0; }
Thu, 25 Aug 2022 04:06:10 -1100
SEBA Class 9th Question Paper 2023,, SEBA Class 9th Question Paper 2023,, SEBA Class 9th Assamese Question Answer, Class 9th Assamese Book Solution, Class 9th Assamese Solutions SEBA Class 9th Assamese Book SEBA. Assam 9th Class Model Paper 2023 Assam 9th Class Model Paper 2023, Assam 9th class Model Question Paper 2019, Assam 9th Sample Question Paper 2019 Assam Formative Assessment(FA) and Summative Assessment (SA) Exam Model Paper 2023.