- 铜板
- 0
- 金币
- 0
- 在线时间
- 0 小时
- 注册时间
- 2006-4-19
- 帖子
- 11

- 铜板
- 0
- 金币
- 0
- 在线时间
- 0 小时
- 注册时间
- 2006-4-19
- 帖子
- 11
|

爱因斯坦的问题
|
<SPAN class=tpc_title>爱因斯坦的问题</SPAN><BR><IMG src="http://www.400.com.cn/images/Emotions/78.gif"><BR><SPAN class=tpc_content>这里有一个很老的题目,看看你能在多长时间内说出答案。 <BR> 问题是:在一条街上,有5座房子,喷了5种颜色;每个房里住着不同国籍的人;每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。请问:谁养鱼? <BR> 提示: <BR> 1. 英国人住红色房子 <BR> 2. 瑞典人养狗 <BR> 3. 丹麦人喝茶 <BR> 4. 绿色房子在白色房子左面 <BR> 5. 绿色房子主人喝咖啡 <BR> 6. 抽pall mall 香烟的人养鸟 <BR> 7. 黄色房子主人抽dunhill 香烟 <BR> 8. 住在中间房子的人喝牛奶 <BR> 9. 挪威人住第一间房 <BR> 10. 抽blends香烟的人住在养猫的人隔壁 <BR> 11. 养马的人住抽dunhill 香烟的人隔壁 <BR> 12. 抽blue master的人喝啤酒 <BR> 13. 德国人抽prince香烟 <BR> 14. 挪威人住蓝色房子隔壁 <BR> 15. 抽blends香烟的人有一个喝水的邻居 <BR><BR> 据说是爱因斯坦提出的一个问题。他老人家的推理能力实在很厉害,俺们普通人就只好用笨方法了。下面给出一个穷举法的程序来计算结果 <BR><BR>#include "stdafx.h" <BR>#include <CONIO.H><BR><BR>char* color_name[] = <BR>{ <BR> "红", "白", "绿", "蓝", "黄", <BR>}; <BR>enum <BR>{ <BR> e_red, e_white, e_green, e_blue, e_yellow <BR>}; <BR><BR>char* nation_name[] = <BR>{ <BR> "英国人", "瑞典人", "丹麦人", "挪威人", "德国人", <BR>}; <BR>enum <BR>{ <BR> e_briton, e_swede, e_dane, e_norwegian, e_german <BR>}; <BR><BR>char* drink_name[] = <BR>{ <BR> "咖啡", "茶", "牛奶", "啤酒", "水", <BR>}; <BR>enum <BR>{ <BR> e_coffee, e_tea, e_milk, e_beer, e_water <BR>}; <BR><BR>char* smoke_name[] = <BR>{ <BR> "pall mall", "dunhill", "blends", "blue master", "prince", <BR>}; <BR>enum <BR>{ <BR> e_pallmall, e_dunhill, e_blends, e_bluemaster, e_prince <BR>}; <BR><BR>char* pet_name[] = <BR>{ <BR> "狗", "猫", "鸟", "马", "鱼", <BR>}; <BR>enum <BR>{ <BR> e_dog, e_cat, e_bird, e_horse, e_fish <BR>}; <BR><BR>const int house_count = 5; <BR>const int attrib_count = 5; <BR>const int tab_count = 5*4*3*2*1; <BR>char attrib_tab[tab_count][house_count]; <BR>struct house_data <BR>{ <BR> union <BR> { <BR> struct <BR> { <BR> int color; <BR> int nation; <BR> int drink; <BR> int smoke; <BR> int pet; <BR> }; <BR> int attrib[5]; <BR> }; <BR>}house; <BR><BR>void house_confirm() <BR>{ <BR> int i; <BR> char* tab_color = attrib_tab[house.color]; <BR> char* tab_nation = attrib_tab[house.nation]; <BR> char* tab_drink = attrib_tab[house.drink]; <BR> char* tab_smoke = attrib_tab[house.smoke]; <BR> char* tab_pet = attrib_tab[house.pet]; <BR><BR> //8. 住在中间房子的人喝牛奶 <BR> if(tab_drink[2] != e_milk) <BR> { <BR> return; <BR> } <BR> for(i = 0; i < house_count; i ++) <BR> { <BR> // 1. 英国人住红色房子 <BR> if(tab_nation<I> == e_briton) <BR> { <BR> if(tab_color<I> != e_red) <BR> { <BR> return; <BR> } <BR> } <BR> //2. 瑞典人养狗 <BR> else if(tab_nation<I> == e_swede) <BR> { <BR> if(tab_pet<I> != e_dog) <BR> { <BR> return; <BR> } <BR> } <BR> //3. 丹麦人喝茶 <BR> else if(tab_nation<I> == e_dane) <BR> { <BR> if(tab_drink<I> != e_tea) <BR> { <BR> return; <BR> } <BR> } <BR> //9. 挪威人住第一间房 <BR> else if(tab_nation<I> == e_norwegian) <BR> { <BR> if(i > 0) <BR> { <BR> return; <BR> } <BR> //14. 挪威人住蓝色房子隔壁 <BR> if(tab_color[1] != e_blue) <BR> { <BR> return; <BR> } <BR> } <BR> //13. 德国人抽prince香烟 <BR> else if(tab_nation<I> == e_german) <BR> { <BR> if(tab_smoke<I> != e_prince) <BR> { <BR> return; <BR> } <BR> } <BR> //4. 绿色房子在白色房子左面 <BR> if(tab_color<I> == e_green) <BR> { <BR> if(i >= house_count) <BR> { <BR> return; <BR> } <BR> if(tab_color[i+1] != e_white) <BR> { <BR> return; <BR> } <BR> //5. 绿色房子主人喝咖啡 <BR> if(tab_drink<I> != e_coffee) <BR> { <BR> return; <BR> } <BR> } <BR> //6. 抽pall mall 香烟的人养鸟 <BR> if(tab_smoke<I> == e_pallmall) <BR> { <BR> if(tab_pet<I> != e_bird) <BR> { <BR> return; <BR> } <BR> } <BR> //7. 黄色房子主人抽dunhill 香烟 <BR> if(tab_color<I> == e_yellow) <BR> { <BR> if(tab_smoke<I> != e_dunhill) <BR> { <BR> return; <BR> } <BR> } <BR> //10. 抽blends香烟的人住在养猫的人隔壁 <BR> //15. 抽blends香烟的人有一个喝水的邻居 <BR> if(tab_smoke<I> == e_blends) <BR> { <BR> bool bCat = false; <BR> bool bWater = false; <BR> if(i < house_count-1) <BR> { <BR> if(tab_pet[i+1] == e_cat) <BR> { <BR> bCat = true; <BR> } <BR> if(tab_drink[i+1] == e_water) <BR> { <BR> bWater = true; <BR> } <BR> } <BR> if(i > 0) <BR> { <BR> if(tab_pet[i-1] == e_cat) <BR> { <BR> bCat = true; <BR> } <BR> if(tab_drink[i-1] == e_water) <BR> { <BR> bWater = true; <BR> } <BR> } <BR> if(!bCat || !bWater) <BR> { <BR> return; <BR> } <BR> } <BR> //11. 养马的人住抽dunhill 香烟的人隔壁 <BR> else if(tab_smoke<I> == e_dunhill) <BR> { <BR> bool bHorse = false; <BR> if(i < house_count-1) <BR> { <BR> if(tab_pet[i+1] == e_horse) <BR> { <BR> bHorse = true; <BR> } <BR> } <BR> if(i > 0) <BR> { <BR> if(tab_pet[i-1] == e_horse) <BR> { <BR> bHorse = true; <BR> } <BR> } <BR> if(!bHorse) <BR> { <BR> return; <BR> } <BR> } <BR> //12. 抽blue master的人喝啤酒 <BR> else if(tab_smoke<I> == e_bluemaster) <BR> { <BR> if(tab_drink<I> != e_beer) <BR> { <BR> return; <BR> } <BR> } <BR> } <BR> printf("Find\n"); <BR><BR> printf("color\tnation\tdrink\tpet\tsmoke\n"); <BR> for(i = 0; i < house_count; i ++) <BR> { <BR> printf("%s\t%s\t%s\t%s\t%s\n", <BR> color_name[tab_color<I>], <BR> nation_name[tab_nation<I>], <BR> drink_name[tab_drink<I>], <BR> pet_name[tab_pet<I>], <BR> smoke_name[tab_smoke<I>] <BR> ); <BR> } <BR> return; <BR>} <BR><BR>void house_walk(int attrib) <BR>{ <BR> int a = attrib + 1; <BR> for(int i = 0; i < tab_count; i ++) <BR> { <BR> house.attrib[attrib] = i; <BR><BR> switch(attrib) <BR> { <BR> // int color; <BR> case 0: <BR> //14. 挪威人住蓝色房子隔壁 <BR> if(attrib_tab<I>[1] != e_blue) <BR> { <BR> continue; <BR> } <BR> break; <BR> // int nation; <BR> case 1: <BR> //9. 挪威人住第一间房 <BR> if(attrib_tab<I>[0] != e_norwegian) <BR> { <BR> continue; <BR> } <BR> break; <BR> // int drink; <BR> case 2: <BR> //8. 住在中间房子的人喝牛奶 <BR> if(attrib_tab<I>[2] != e_milk) <BR> { <BR> continue; <BR> } <BR> { <BR> bool bContinue = false; <BR> for(int h = 0; h < house_count; h ++) <BR> { <BR> if(attrib_tab<I>[h] == e_tea) <BR> { <BR> if(attrib_tab[house.nation][h] != e_dane) <BR> { <BR> bContinue = true; <BR> break; <BR> } <BR> } <BR> else if(attrib_tab<I>[h] == e_coffee) <BR> { <BR> if(attrib_tab[house.color][h] != e_green) <BR> { <BR> bContinue = true; <BR> break; <BR> } <BR> } <BR> } <BR> if(bContinue) <BR> { <BR> continue; <BR> } <BR> } <BR> break; <BR> // int smoke; <BR> case 3: <BR> { <BR> bool bContinue = false; <BR> for(int h = 0; h < house_count; h ++) <BR> { <BR> if(attrib_tab<I>[h] == e_dunhill) <BR> { <BR> if(attrib_tab[house.color][h] != e_yellow) <BR> { <BR> bContinue = true; <BR> break; <BR> } <BR> } <BR> else if(attrib_tab<I>[h] == e_bluemaster) <BR> { <BR> if(attrib_tab[house.drink][h] != e_beer) <BR> { <BR> bContinue = true; <BR> break; <BR> } <BR> } <BR> } <BR> if(bContinue) <BR> { <BR> continue; <BR> } <BR> } <BR> break; <BR> // int pet; <BR> } <BR> <BR> if(a >= attrib_count) <BR> { <BR> house_confirm(); <BR> } <BR> else <BR> { <BR> house_walk(attrib+1); <BR> } <BR> } <BR>} <BR><BR>void gen_tab(int h, int& index) <BR>{ <BR> int i; <BR> for(i = 0; i < house_count; i ++) <BR> { <BR> int j; <BR> for(j = 0; j < h; j ++) <BR> { <BR> if(i == attrib_tab[index][j]) <BR> { <BR> goto lable_next; <BR> } <BR> } <BR> attrib_tab[index][h] = i; <BR> if(h == house_count-1) <BR> { <BR> index ++; <BR> attrib_tab[index][0] = attrib_tab[index-1][0]; <BR> attrib_tab[index][1] = attrib_tab[index-1][1]; <BR> attrib_tab[index][2] = attrib_tab[index-1][2]; <BR> attrib_tab[index][3] = attrib_tab[index-1][3]; <BR> return; <BR> } <BR> gen_tab(h+1, index); <BR>lable_next:; <BR> } <BR>} <BR><BR>int _tmain(int argc, _TCHAR* argv[]) <BR>{ <BR> int index = 0; <BR> gen_tab(0, index); <BR> house_walk(0); <BR> getch(); <BR> return 0; <BR>} <BR><BR></SPAN><BR></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I></I> |
|