找回密码
 欢迎注册
楼主: gxqcn

[分享] 试试你有多聪明——经典逻辑智力题50例

  [复制链接]
 楼主| 发表于 2008-9-18 07:34:29 | 显示全部楼层
原帖由 zYr 于 2008-9-17 22:14 发表 这“经典逻辑智力题50例”都快做完了,能不能再出个几十例?
你可以试试由 shability 整理的《我整理的一些IQ题目合集》, 我记得里面并未完全解答。 还有 无心人 发的《数学奥林匹克升级题》,也非常有意思。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-9-18 10:00:17 | 显示全部楼层
原帖由 gxqcn 于 2008-6-2 10:28 发表 爱因斯坦出了一道题,他说世界上有90%的人回答不出,看看你是否属于10%。 内容: 1、在一条街上,有5座房子,喷了5种颜色。 2、每个房里住着不同国籍的人。 3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物。 已知条件: 1、英国人住红色房子 2、瑞典人养狗 3、丹麦人喝茶 4、绿色房子在白色房子左面 5、绿色房子主人喝咖啡 6、抽Pall Mall香烟的人养鸟 7、黄色房子主人抽Dunhill香烟 8、住在中间房子的人喝牛奶 9、挪威人住第一间房 10、抽混合烟的人住在养猫的人隔壁 11、养马的人住在抽Dunhill香烟的人隔壁 12、抽Blue Master的人喝啤酒 13、德国人抽Prince香烟 14、挪威人住蓝色房子隔壁 15、抽混合烟的人有一个喝水的邻居 问题:谁养鱼?
以前看这个题目的时候,说的是98%的人都回答不出来,不知为何,现在变成了了90%。 根据题意,由于没有明确给出一个人的名字,姑且都以国籍来区别不同人的姓名,如英国人、德国人等等,那么关于人的属性中,有如下几个字段: 房子的位置:posi 名字: name 房子的颜色:color 饮料:drink 香烟:cig 宠物:pet 这里有一个问题, 就是到底该以哪一个进行编号呢? 名字? 颜色? 饮料? 香烟? 宠物? 这些都有问题, 因为他们之间没有非常明确的顺序, 最后剩下的就是房子的位置了, 把它们编成0~4号 编号: 0 1 2 3 4 posi: P0 P1 P2 P3 P4 剩下的五个字段:name、color、drink、cig、pet也需要进行编号(0~4),但是他们的号码还未知,需要我们去推导, name: 英国人 瑞典人 丹麦人 挪威人 德国人 color: 红 绿 白 黄 蓝 drink: 茶 咖啡 牛奶 啤酒 水 cig: PALLMAL DUNHILL 混合烟 BLUEMAS PRINCE pet: 狗 鸟 猫 马 鱼 推导后,如果我们得出“英国人 = 0, 红 = 0, 茶 = 0,PALL = 0, 狗 = 0”,就表明了英国人住第0间的红房子,喝茶,抽PALLMALL, 养狗。 再根据条件1-15,可以得到如下方程: 1. --> 英国人 = 红 (1) 2. --> 瑞典人 = 狗 (2) <严重申明: 本人绝对没有侮辱的意思> 3. --> 丹麦人 = 茶 (3) 4. --> 绿+1 = 白 (4) 5. --> 绿 = 咖啡 (5) 6. --> PALLMAL = 鸟 (6) 7. --> 黄 = DUNHILL (7) 8. --> 牛奶 = 2 (8) 9. --> 挪威人 = 0 (9) 10 --> |混合烟-猫| = 1 (a) 11 --> |马-DUNHILL| = 1 (b) 12 --> BLUEMAS = 啤酒 (c) 13 --> 德国人 = PRINCE (d) 14 --> |挪威人-蓝| = 1 (e) 15 --> |混合烟-水| = 1 (f) 直观上,可以由(8), (9)可得: posi: 0 1 2 3 4 name: 挪威人 color: drink: 牛奶 cig: pet: 消去上述用过的条件,并整理相似条件,得: 1. --> 英国人 = 红 (1) 2. --> 瑞典人 = 狗 (2) <严重申明: 本人绝对没有侮辱的意思> 3. --> 丹麦人 = 茶 (3) 5. --> 绿 = 咖啡 (5) 6. --> PALLMAL = 鸟 (6) 7. --> 黄 = DUNHILL (7) 4. --> 绿+1 = 白 (4) 12 --> BLUEMAS = 啤酒 (c) 13 --> 德国人 = PRINCE (d) 10 --> |混合烟-猫| = 1 (a) 11 --> |马-DUNHILL| = 1 (b) 14 --> |挪威人-蓝| = 1 (e) 15 --> |混合烟-水| = 1 (f) 根据(e), 得 蓝 = 1 (g) 根据(4), 现大胆假设 绿 = 2 (h) 根据(5), 知咖啡与牛奶冲突, 假设失败 根据(h), 得 绿 = 3 (i) 根据(4), 得 白 = 4 (j) 根据(5), 得 咖啡 = 3 (k) 根据(1), 得 英国人 = 红 = 2 (l) 根据(l), 得 黄 = 0 (m) 根据(7), 得 DUNHILL = 0 (n) 根据(b), 得 马 = 1 (o) 到此位置, 我们得到了如下的推断表及剩余的条件: posi: 0 1 2 3 4 name: 挪威人 英国人 color: 黄 蓝 红 绿 白 drink: 牛奶 咖啡 cig: DUNHILL pet: 马 2. --> 瑞典人 = 狗 (2) <严重申明: 本人绝对没有侮辱的意思> 3. --> 丹麦人 = 茶 (3) 6. --> PALLMAL = 鸟 (6) 12 --> BLUEMAS = 啤酒 (c) 13 --> 德国人 = PRINCE (d) 10 --> |混合烟-猫| = 1 (a) 15 --> |混合烟-水| = 1 (f) 根据(3), 假设 丹麦人 = 4 (p) 根据(3), 得 茶 = 4 (p-1) 根据(c), 得 BLUEMAS = 啤酒 = 1 (p-2) 根据(2), 得 瑞典人 = 狗 = 3 (p-3) 根据(6), 知无法安排它们的值, 冲突, (p)的假设失败 根据(p), 得 丹麦人 = 1 (q) 根据(3), 得 茶 = 1 (r) 根据(c), 得 BLUEMAS = 啤酒 = 4 (s) 根据(d), 得 德国人 = PRINCE = 3 (t) 根据(t)及(2), 得 瑞典人 = 狗 = 5 (u) 根据(6), 得 PALLMAL = 鸟 = 2 (v) 根据(s), 得 水 = 0 (w) 根据(v)或(f), 得 混合烟 = 1 (x) 根据(a), 得 猫= 1 (y) 根据(y), 得 鱼 = 3 (z) 最后的排列如下: posi: 0 1 2 3 4 name: 挪威人 丹麦人 英国人 德国人 瑞典人 color: 黄 蓝 红 绿 白 drink: 水 茶 牛奶 咖啡 啤酒 cig: Dunhill 混合烟 PallMal Prince BlueMas pet: 猫 马 鸟 鱼 狗 即德国人住第四栋(3+1)的绿房子, 喝咖啡, 抽PRINCE, 养鱼 同时由(x), 可知, 第(f)个条件是多余的. 即不要第15个条件, 照样可以推出正确的结果!
源代码如下:
  1. /************
  2. * 关于爱因斯坦98%的人都回答不出来的粗浅程序, 鉴于网络上没有很好的C参考程序, \
  3. * 这里尝试写一个, 聊博一笑! *^_^*, 请在VC6.0以上环境编译!
  4. * Email: silitex@yeah.net
  5. * 注: 如采用原始枚举: (5!)^5 = 24,883,200,000, 在普通的电脑需运行1分钟以上, \
  6. * 故做一个算法优化, 在枚举时, 把已知的2个条件: 牛奶 = 2, 挪威人 = 0, \
  7. * 直接确定; 这样算法的枚举次数为: (4!)^2*(5!)^3 = 995,328,000.
  8. * 用C++的标准做法应该把所有的内容封装成类, 否则太容易因为下表取错而出错!
  9. ***/
  10. #include <iostream>
  11. #include <stdlib.h>
  12. using namespace std;
  13. #define N 5
  14. #define N_NAME 24 // Name的枚举次数
  15. #define N_COLOR 120 // ...
  16. #define N_DRINK 24 // ...
  17. #define N_CIG 120 // ...
  18. #define N_PET 120 // ...
  19. enum E_NAME {ENGLISH, SWEDISH, DANE, NORWEGIAN, GERMAN};
  20. enum E_COLOR {RED, GREEN, WHITE, YELLOW, BLUE};
  21. enum E_DRINK {TEA, COFFEE, MILK, BEER, WATER};
  22. enum E_CIGARETTE {PALLMALL, DUNHILL, BLENDS, BLUEMASTER, PRINCE}; // Cigar类?
  23. enum E_PET {DOG, BIRD, CAT, HORSE, FISH};
  24. const char *NAME[] = {"英国人", "瑞典人", "丹麦人", "挪威人", "德国人"};// 人名
  25. const char *COLOR[] = {"红", "绿", "白", "黄", "蓝"}; // 颜色
  26. const char *DRINK[] = {"茶", "咖啡", "牛奶", "啤酒", "水"}; // 饮料
  27. const char *CIGARETTE[] = {"PallMal", "Dunhill", "混合烟", "BlueMas", \
  28. "Prince"}; // 香烟
  29. const char *PET[] = {"狗", "鸟", "猫", "马", "鱼"}; // 宠物
  30. extern const int ENUM_NAME[][N];
  31. extern const int ENUM_DRINK[][N];
  32. extern const int ENUM_ALL[][N];
  33. int main(void)
  34. {
  35. int tmp_name[N]; // 临时数组, 以上面的枚举变量为索引, 指示在第几栋房子
  36. int tmp_color[N];
  37. int tmp_drink[N];
  38. int tmp_cig[N];
  39. int tmp_pet[N];
  40. int out_name[N]; // 输出数组, 以第几栋房子为位置, 记录上面的枚举变量值
  41. int out_color[N];
  42. int out_drink[N];
  43. int out_cig[N];
  44. int out_pet[N];
  45. int i;
  46. long per;
  47. for (int n = 0; n < N_NAME; n++)
  48. {
  49. for (i = 0; i < N; i++)
  50. tmp_name[ENUM_NAME[n][i]] = i;
  51. for (int c = 0; c < N_COLOR; c++)
  52. {
  53. for (i = 0; i < N; i++)
  54. tmp_color[i] = ENUM_ALL[c][i];
  55. for (int d = 0; d < N_DRINK; d++)
  56. {
  57. for (i = 0; i < N; i++)
  58. tmp_drink[ENUM_DRINK[d][i]] = i;
  59. for (int c2 = 0; c2 < N_CIG; c2++)
  60. {
  61. for (i = 0; i < N; i++)
  62. tmp_cig[i] = ENUM_ALL[c2][i];
  63. for (int p = 0; p < N_PET; p++)
  64. {
  65. for (i = 0; i < N; i++)
  66. tmp_pet[i] = ENUM_ALL[p][i];
  67. /* 前面的就是枚举所有可能行, 下面的工作就根据条件判断\
  68. * 已知我们用掉了5个初始条件, 所以这里判断10个剩余条件.
  69. */
  70. if (tmp_name[ENGLISH] != tmp_color[RED])
  71. continue;
  72. if (tmp_name[SWEDISH] != tmp_pet[DOG])
  73. continue;
  74. if (tmp_name[DANE] != tmp_drink[TEA])
  75. continue;
  76. if (tmp_color[GREEN] != tmp_drink[COFFEE])
  77. continue;
  78. if (tmp_cig[PALLMALL] != tmp_pet[BIRD])
  79. continue;
  80. if (tmp_color[YELLOW] != tmp_cig[DUNHILL])
  81. continue;
  82. if (tmp_cig[BLUEMASTER] != tmp_drink[BEER])
  83. continue;
  84. if (tmp_name[GERMAN] != tmp_cig[PRINCE])
  85. continue;
  86. #if 1 // 绿在白的左边, 且必须为邻居
  87. if (tmp_color[GREEN]+1 != tmp_color[WHITE])
  88. continue;
  89. #else // 绿在白的左边, 不必为邻居
  90. if (tmp_color[GREEN] >= tmp_color[WHITE])
  91. continue;
  92. #endif
  93. if (abs(tmp_cig[BLENDS]-tmp_pet[CAT]) != 1)
  94. continue;
  95. if (abs(tmp_pet[HORSE]-tmp_cig[DUNHILL]) != 1)
  96. continue;
  97. if (abs(tmp_name[NORWEGIAN]-tmp_color[BLUE]) != 1)
  98. continue;
  99. #if 1 // 这个条件多余, 可以不用.
  100. if (abs(tmp_cig[BLENDS]-tmp_drink[WATER]) != 1)
  101. continue;
  102. #endif
  103. // 如果上面的条件都满足, 则显示出来
  104. for (i = 0; i < N; i++)
  105. {
  106. out_name[tmp_name[i]] = i;
  107. out_color[tmp_color[i]] = i;
  108. out_drink[tmp_drink[i]] = i;
  109. out_cig[tmp_cig[i]] = i;
  110. out_pet[tmp_pet[i]] = i;
  111. }
  112. printf("\nposi: 0 1 2 3 4");
  113. printf("\nname: %-7s %-7s %-7s %-7s %-7s\n", \
  114. NAME[out_name[0]], NAME[out_name[1]], \
  115. NAME[out_name[2]], NAME[out_name[3]], \
  116. NAME[out_name[4]]);
  117. printf("color: %-7s %-7s %-7s %-7s %-7s\n", \
  118. COLOR[out_color[0]], COLOR[out_color[1]], \
  119. COLOR[out_color[2]], COLOR[out_color[3]], \
  120. COLOR[out_color[4]]);
  121. printf("drink: %-7s %-7s %-7s %-7s %-7s\n", \
  122. DRINK[out_drink[0]], DRINK[out_drink[1]], \
  123. DRINK[out_drink[2]], DRINK[out_drink[3]], \
  124. DRINK[out_drink[4]]);
  125. printf("cig: %-7s %-7s %-7s %-7s %-7s\n", \
  126. CIGARETTE[out_cig[0]], CIGARETTE[out_cig[1]], \
  127. CIGARETTE[out_cig[2]], CIGARETTE[out_cig[3]], \
  128. CIGARETTE[out_cig[4]]);
  129. printf("pet: %-7s %-7s %-7s %-7s %-7s\n", \
  130. PET[out_pet[0]], PET[out_pet[1]], \
  131. PET[out_pet[2]], PET[out_pet[3]], \
  132. PET[out_pet[4]]);
  133. }
  134. }
  135. }
  136. // 显示当前的进度
  137. per = ((N_COLOR*(long)n)+(long)c+1)*100/(N_NAME*N_COLOR);
  138. printf("\rProgress: %02d%% ", per);
  139. }
  140. }
  141. return 0;
  142. }
  143. const int ENUM_NAME[][N] = {
  144. {NORWEGIAN, ENGLISH, SWEDISH, DANE , GERMAN },
  145. {NORWEGIAN, ENGLISH, SWEDISH, GERMAN , DANE },
  146. {NORWEGIAN, ENGLISH, DANE , SWEDISH, GERMAN },
  147. {NORWEGIAN, ENGLISH, DANE , GERMAN , SWEDISH},
  148. {NORWEGIAN, ENGLISH, GERMAN , SWEDISH, DANE },
  149. {NORWEGIAN, ENGLISH, GERMAN , DANE , SWEDISH},
  150. {NORWEGIAN, SWEDISH, ENGLISH, DANE , GERMAN },
  151. {NORWEGIAN, SWEDISH, ENGLISH, GERMAN , DANE },
  152. {NORWEGIAN, SWEDISH, DANE , ENGLISH, GERMAN },
  153. {NORWEGIAN, SWEDISH, DANE , GERMAN , ENGLISH},
  154. {NORWEGIAN, SWEDISH, GERMAN , ENGLISH, DANE },
  155. {NORWEGIAN, SWEDISH, GERMAN , DANE , ENGLISH},
  156. {NORWEGIAN, DANE , ENGLISH, SWEDISH, GERMAN },
  157. {NORWEGIAN, DANE , ENGLISH, GERMAN , SWEDISH},
  158. {NORWEGIAN, DANE , SWEDISH, ENGLISH, GERMAN },
  159. {NORWEGIAN, DANE , SWEDISH, GERMAN , ENGLISH},
  160. {NORWEGIAN, DANE , GERMAN , ENGLISH, SWEDISH},
  161. {NORWEGIAN, DANE , GERMAN , SWEDISH, ENGLISH},
  162. {NORWEGIAN, GERMAN , ENGLISH, SWEDISH, DANE },
  163. {NORWEGIAN, GERMAN , ENGLISH, DANE , SWEDISH},
  164. {NORWEGIAN, GERMAN , SWEDISH, ENGLISH, DANE },
  165. {NORWEGIAN, GERMAN , SWEDISH, DANE , ENGLISH},
  166. {NORWEGIAN, GERMAN , DANE , ENGLISH, SWEDISH},
  167. {NORWEGIAN, GERMAN , DANE , SWEDISH, ENGLISH},
  168. };
  169. const int ENUM_DRINK[][N] = {
  170. {TEA , COFFEE , MILK , BEER , WATER },
  171. {TEA , COFFEE , MILK , WATER , BEER },
  172. {TEA , BEER , MILK , COFFEE , WATER },
  173. {TEA , BEER , MILK , WATER , COFFEE },
  174. {TEA , WATER , MILK , COFFEE , BEER },
  175. {TEA , WATER , MILK , BEER , COFFEE },
  176. {COFFEE , TEA , MILK , BEER , WATER },
  177. {COFFEE , TEA , MILK , WATER , BEER },
  178. {COFFEE , BEER , MILK , TEA , WATER },
  179. {COFFEE , BEER , MILK , WATER , TEA },
  180. {COFFEE , WATER , MILK , TEA , BEER },
  181. {COFFEE , WATER , MILK , BEER , TEA },
  182. {BEER , TEA , MILK , COFFEE , WATER },
  183. {BEER , TEA , MILK , WATER , COFFEE },
  184. {BEER , COFFEE , MILK , TEA , WATER },
  185. {BEER , COFFEE , MILK , WATER , TEA },
  186. {BEER , WATER , MILK , TEA , COFFEE },
  187. {BEER , WATER , MILK , COFFEE , TEA },
  188. {WATER , TEA , MILK , COFFEE , BEER },
  189. {WATER , TEA , MILK , BEER , COFFEE },
  190. {WATER , COFFEE , MILK , TEA , BEER },
  191. {WATER , COFFEE , MILK , BEER , TEA },
  192. {WATER , BEER , MILK , TEA , COFFEE },
  193. {WATER , BEER , MILK , COFFEE , TEA },
  194. };
  195. const int ENUM_ALL[][N] = {
  196. {0,1,2,3,4}, {0,1,2,4,3}, {0,1,3,2,4}, {0,1,3,4,2}, {0,1,4,2,3},
  197. {0,1,4,3,2}, {0,2,1,3,4}, {0,2,1,4,3}, {0,2,3,1,4}, {0,2,3,4,1},
  198. {0,2,4,1,3}, {0,2,4,3,1}, {0,3,1,2,4}, {0,3,1,4,2}, {0,3,2,1,4},
  199. {0,3,2,4,1}, {0,3,4,1,2}, {0,3,4,2,1}, {0,4,1,2,3}, {0,4,1,3,2},
  200. {0,4,2,1,3}, {0,4,2,3,1}, {0,4,3,1,2}, {0,4,3,2,1}, {1,0,2,3,4},
  201. {1,0,2,4,3}, {1,0,3,2,4}, {1,0,3,4,2}, {1,0,4,2,3}, {1,0,4,3,2},
  202. {1,2,0,3,4}, {1,2,0,4,3}, {1,2,3,0,4}, {1,2,3,4,0}, {1,2,4,0,3},
  203. {1,2,4,3,0}, {1,3,0,2,4}, {1,3,0,4,2}, {1,3,2,0,4}, {1,3,2,4,0},
  204. {1,3,4,0,2}, {1,3,4,2,0}, {1,4,0,2,3}, {1,4,0,3,2}, {1,4,2,0,3},
  205. {1,4,2,3,0}, {1,4,3,0,2}, {1,4,3,2,0}, {2,0,1,3,4}, {2,0,1,4,3},
  206. {2,0,3,1,4}, {2,0,3,4,1}, {2,0,4,1,3}, {2,0,4,3,1}, {2,1,0,3,4},
  207. {2,1,0,4,3}, {2,1,3,0,4}, {2,1,3,4,0}, {2,1,4,0,3}, {2,1,4,3,0},
  208. {2,3,0,1,4}, {2,3,0,4,1}, {2,3,1,0,4}, {2,3,1,4,0}, {2,3,4,0,1},
  209. {2,3,4,1,0}, {2,4,0,1,3}, {2,4,0,3,1}, {2,4,1,0,3}, {2,4,1,3,0},
  210. {2,4,3,0,1}, {2,4,3,1,0}, {3,0,1,2,4}, {3,0,1,4,2}, {3,0,2,1,4},
  211. {3,0,2,4,1}, {3,0,4,1,2}, {3,0,4,2,1}, {3,1,0,2,4}, {3,1,0,4,2},
  212. {3,1,2,0,4}, {3,1,2,4,0}, {3,1,4,0,2}, {3,1,4,2,0}, {3,2,0,1,4},
  213. {3,2,0,4,1}, {3,2,1,0,4}, {3,2,1,4,0}, {3,2,4,0,1}, {3,2,4,1,0},
  214. {3,4,0,1,2}, {3,4,0,2,1}, {3,4,1,0,2}, {3,4,1,2,0}, {3,4,2,0,1},
  215. {3,4,2,1,0}, {4,0,1,2,3}, {4,0,1,3,2}, {4,0,2,1,3}, {4,0,2,3,1},
  216. {4,0,3,1,2}, {4,0,3,2,1}, {4,1,0,2,3}, {4,1,0,3,2}, {4,1,2,0,3},
  217. {4,1,2,3,0}, {4,1,3,0,2}, {4,1,3,2,0}, {4,2,0,1,3}, {4,2,0,3,1},
  218. {4,2,1,0,3}, {4,2,1,3,0}, {4,2,3,0,1}, {4,2,3,1,0}, {4,3,0,1,2},
  219. {4,3,0,2,1}, {4,3,1,0,2}, {4,3,1,2,0}, {4,3,2,0,1}, {4,3,2,1,0},
  220. };
复制代码

评分

参与人数 1威望 +2 鲜花 +2 收起 理由
gxqcn + 2 + 2 算法很好;排版很精美

查看全部评分

毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-9-18 15:22:19 | 显示全部楼层
牛 不过,我觉得还是函数式语言更合适
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-9-18 15:31:42 | 显示全部楼层
哦, 函数式语言, 没用过! 似有前人用函数式语言写过的样子!
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-9-18 19:56:01 | 显示全部楼层

回复 182# silitex 的帖子

刚才在 VC6.0 下编译出现了大量 error,就略作了下修改:
  1. /************
  2. * 关于爱因斯坦98%的人都回答不出来的粗浅程序, 鉴于网络上没有很好的C参考程序, \
  3. * 这里尝试写一个, 聊博一笑! *^_^*, 请在VC6.0或以上环境编译!
  4. * Email: silitex@yeah.net
  5. * 注: 如采用原始枚举: (5!)^5 = 24,883,200,000, 在普通的电脑需运行1分钟以上, \
  6. * 故做一个算法优化, 在枚举时, 把已知的2个条件: 牛奶 = 2, 挪威人 = 0, \
  7. * 直接确定; 这样算法的枚举次数为: (4!)^2*(5!)^3 = 995,328,000.
  8. * 用C++的标准做法应该把所有的内容封装成类, 否则太容易因为下表取错而出错!
  9. ***/
  10. #include <stdio.h>
  11. #include <math.h>
  12. #define N 5
  13. #define N_NAME 24 /* Name的枚举次数 */
  14. #define N_COLOR 120
  15. #define N_DRINK 24
  16. #define N_CIG 120
  17. #define N_PET 120
  18. enum E_NAME {ENGLISH, SWEDISH, DANE, NORWEGIAN, GERMAN};
  19. enum E_COLOR {RED, GREEN, WHITE, YELLOW, BLUE};
  20. enum E_DRINK {TEA, COFFEE, MILK, BEER, WATER};
  21. enum E_CIGARETTE {PALLMALL, DUNHILL, BLENDS, BLUEMASTER, PRINCE}; // Cigar类?
  22. enum E_PET {DOG, BIRD, CAT, HORSE, FISH};
  23. const char *NAME[] = {"英国人", "瑞典人", "丹麦人", "挪威人", "德国人"};// 人名
  24. const char *COLOR[] = {"红", "绿", "白", "黄", "蓝"}; // 颜色
  25. const char *DRINK[] = {"茶", "咖啡", "牛奶", "啤酒", "水"}; // 饮料
  26. const char *CIGARETTE[] = {"PallMal", "Dunhill", "混合烟", "BlueMas", \
  27. "Prince"}; // 香烟
  28. const char *PET[] = {"狗", "鸟", "猫", "马", "鱼"}; // 宠物
  29. extern const int ENUM_NAME[][N];
  30. extern const int ENUM_DRINK[][N];
  31. extern const int ENUM_ALL[][N];
  32. int main(void)
  33. {
  34. int tmp_name[N]; // 临时数组, 以上面的枚举变量为索引, 指示在第几栋房子
  35. int tmp_color[N];
  36. int tmp_drink[N];
  37. int tmp_cig[N];
  38. int tmp_pet[N];
  39. int out_name[N]; // 输出数组, 以第几栋房子为位置, 记录上面的枚举变量值
  40. int out_color[N];
  41. int out_drink[N];
  42. int out_cig[N];
  43. int out_pet[N];
  44. int i;
  45. long per;
  46. for (int n = 0; n < N_NAME; n++)
  47. {
  48. for (i = 0; i < N; i++)
  49. tmp_name[ENUM_NAME[n][i]] = i;
  50. for (int c = 0; c < N_COLOR; c++)
  51. {
  52. for (i = 0; i < N; i++)
  53. tmp_color[i] = ENUM_ALL[c][i];
  54. for (int d = 0; d < N_DRINK; d++)
  55. {
  56. for (i = 0; i < N; i++)
  57. tmp_drink[ENUM_DRINK[d][i]] = i;
  58. for (int c2 = 0; c2 < N_CIG; c2++)
  59. {
  60. for (i = 0; i < N; i++)
  61. tmp_cig[i] = ENUM_ALL[c2][i];
  62. for (int p = 0; p < N_PET; p++)
  63. {
  64. for (i = 0; i < N; i++)
  65. tmp_pet[i] = ENUM_ALL[p][i];
  66. /* 前面的就是枚举所有可能行, 下面的工作就根据条件判断\
  67. * 已知我们用掉了5个初始条件, 所以这里判断10个剩余条件.
  68. */
  69. if (tmp_name[ENGLISH] != tmp_color[RED])
  70. continue;
  71. if (tmp_name[SWEDISH] != tmp_pet[DOG])
  72. continue;
  73. if (tmp_name[DANE] != tmp_drink[TEA])
  74. continue;
  75. if (tmp_color[GREEN] != tmp_drink[COFFEE])
  76. continue;
  77. if (tmp_cig[PALLMALL] != tmp_pet[BIRD])
  78. continue;
  79. if (tmp_color[YELLOW] != tmp_cig[DUNHILL])
  80. continue;
  81. if (tmp_cig[BLUEMASTER] != tmp_drink[BEER])
  82. continue;
  83. if (tmp_name[GERMAN] != tmp_cig[PRINCE])
  84. continue;
  85. #if 1 // 绿在白的左边, 且必须为邻居
  86. if (tmp_color[GREEN]+1 != tmp_color[WHITE])
  87. continue;
  88. #else // 绿在白的左边, 不必为邻居
  89. if (tmp_color[GREEN] >= tmp_color[WHITE])
  90. continue;
  91. #endif
  92. if (abs(tmp_cig[BLENDS]-tmp_pet[CAT]) != 1)
  93. continue;
  94. if (abs(tmp_pet[HORSE]-tmp_cig[DUNHILL]) != 1)
  95. continue;
  96. if (abs(tmp_name[NORWEGIAN]-tmp_color[BLUE]) != 1)
  97. continue;
  98. #if 1 // 这个条件多余, 可以不用.
  99. if (abs(tmp_cig[BLENDS]-tmp_drink[WATER]) != 1)
  100. continue;
  101. #endif
  102. // 如果上面的条件都满足, 则显示出来
  103. for (i = 0; i < N; i++)
  104. {
  105. out_name[tmp_name[i]] = i;
  106. out_color[tmp_color[i]] = i;
  107. out_drink[tmp_drink[i]] = i;
  108. out_cig[tmp_cig[i]] = i;
  109. out_pet[tmp_pet[i]] = i;
  110. }
  111. printf("\nposi: 0 1 2 3 4");
  112. printf("\nname: %-7s %-7s %-7s %-7s %-7s\n", \
  113. NAME[out_name[0]], NAME[out_name[1]], \
  114. NAME[out_name[2]], NAME[out_name[3]], \
  115. NAME[out_name[4]]);
  116. printf("color: %-7s %-7s %-7s %-7s %-7s\n", \
  117. COLOR[out_color[0]], COLOR[out_color[1]], \
  118. COLOR[out_color[2]], COLOR[out_color[3]], \
  119. COLOR[out_color[4]]);
  120. printf("drink: %-7s %-7s %-7s %-7s %-7s\n", \
  121. DRINK[out_drink[0]], DRINK[out_drink[1]], \
  122. DRINK[out_drink[2]], DRINK[out_drink[3]], \
  123. DRINK[out_drink[4]]);
  124. printf("cig: %-7s %-7s %-7s %-7s %-7s\n", \
  125. CIGARETTE[out_cig[0]], CIGARETTE[out_cig[1]], \
  126. CIGARETTE[out_cig[2]], CIGARETTE[out_cig[3]], \
  127. CIGARETTE[out_cig[4]]);
  128. printf("pet: %-7s %-7s %-7s %-7s %-7s\n", \
  129. PET[out_pet[0]], PET[out_pet[1]], \
  130. PET[out_pet[2]], PET[out_pet[3]], \
  131. PET[out_pet[4]]);
  132. }
  133. }
  134. }
  135. // 显示当前的进度
  136. per = ((N_COLOR*(long)n)+(long)c+1)*100/(N_NAME*N_COLOR);
  137. printf("\rProgress: %02d%% ", per);
  138. }
  139. }
  140. return 0;
  141. }
  142. const int ENUM_NAME[][N] = {
  143. {NORWEGIAN, ENGLISH, SWEDISH, DANE , GERMAN },
  144. {NORWEGIAN, ENGLISH, SWEDISH, GERMAN , DANE },
  145. {NORWEGIAN, ENGLISH, DANE , SWEDISH, GERMAN },
  146. {NORWEGIAN, ENGLISH, DANE , GERMAN , SWEDISH},
  147. {NORWEGIAN, ENGLISH, GERMAN , SWEDISH, DANE },
  148. {NORWEGIAN, ENGLISH, GERMAN , DANE , SWEDISH},
  149. {NORWEGIAN, SWEDISH, ENGLISH, DANE , GERMAN },
  150. {NORWEGIAN, SWEDISH, ENGLISH, GERMAN , DANE },
  151. {NORWEGIAN, SWEDISH, DANE , ENGLISH, GERMAN },
  152. {NORWEGIAN, SWEDISH, DANE , GERMAN , ENGLISH},
  153. {NORWEGIAN, SWEDISH, GERMAN , ENGLISH, DANE },
  154. {NORWEGIAN, SWEDISH, GERMAN , DANE , ENGLISH},
  155. {NORWEGIAN, DANE , ENGLISH, SWEDISH, GERMAN },
  156. {NORWEGIAN, DANE , ENGLISH, GERMAN , SWEDISH},
  157. {NORWEGIAN, DANE , SWEDISH, ENGLISH, GERMAN },
  158. {NORWEGIAN, DANE , SWEDISH, GERMAN , ENGLISH},
  159. {NORWEGIAN, DANE , GERMAN , ENGLISH, SWEDISH},
  160. {NORWEGIAN, DANE , GERMAN , SWEDISH, ENGLISH},
  161. {NORWEGIAN, GERMAN , ENGLISH, SWEDISH, DANE },
  162. {NORWEGIAN, GERMAN , ENGLISH, DANE , SWEDISH},
  163. {NORWEGIAN, GERMAN , SWEDISH, ENGLISH, DANE },
  164. {NORWEGIAN, GERMAN , SWEDISH, DANE , ENGLISH},
  165. {NORWEGIAN, GERMAN , DANE , ENGLISH, SWEDISH},
  166. {NORWEGIAN, GERMAN , DANE , SWEDISH, ENGLISH},
  167. };
  168. const int ENUM_DRINK[][N] = {
  169. {TEA , COFFEE , MILK , BEER , WATER },
  170. {TEA , COFFEE , MILK , WATER , BEER },
  171. {TEA , BEER , MILK , COFFEE , WATER },
  172. {TEA , BEER , MILK , WATER , COFFEE },
  173. {TEA , WATER , MILK , COFFEE , BEER },
  174. {TEA , WATER , MILK , BEER , COFFEE },
  175. {COFFEE , TEA , MILK , BEER , WATER },
  176. {COFFEE , TEA , MILK , WATER , BEER },
  177. {COFFEE , BEER , MILK , TEA , WATER },
  178. {COFFEE , BEER , MILK , WATER , TEA },
  179. {COFFEE , WATER , MILK , TEA , BEER },
  180. {COFFEE , WATER , MILK , BEER , TEA },
  181. {BEER , TEA , MILK , COFFEE , WATER },
  182. {BEER , TEA , MILK , WATER , COFFEE },
  183. {BEER , COFFEE , MILK , TEA , WATER },
  184. {BEER , COFFEE , MILK , WATER , TEA },
  185. {BEER , WATER , MILK , TEA , COFFEE },
  186. {BEER , WATER , MILK , COFFEE , TEA },
  187. {WATER , TEA , MILK , COFFEE , BEER },
  188. {WATER , TEA , MILK , BEER , COFFEE },
  189. {WATER , COFFEE , MILK , TEA , BEER },
  190. {WATER , COFFEE , MILK , BEER , TEA },
  191. {WATER , BEER , MILK , TEA , COFFEE },
  192. {WATER , BEER , MILK , COFFEE , TEA },
  193. };
  194. const int ENUM_ALL[][N] = {
  195. {0,1,2,3,4}, {0,1,2,4,3}, {0,1,3,2,4}, {0,1,3,4,2}, {0,1,4,2,3},
  196. {0,1,4,3,2}, {0,2,1,3,4}, {0,2,1,4,3}, {0,2,3,1,4}, {0,2,3,4,1},
  197. {0,2,4,1,3}, {0,2,4,3,1}, {0,3,1,2,4}, {0,3,1,4,2}, {0,3,2,1,4},
  198. {0,3,2,4,1}, {0,3,4,1,2}, {0,3,4,2,1}, {0,4,1,2,3}, {0,4,1,3,2},
  199. {0,4,2,1,3}, {0,4,2,3,1}, {0,4,3,1,2}, {0,4,3,2,1}, {1,0,2,3,4},
  200. {1,0,2,4,3}, {1,0,3,2,4}, {1,0,3,4,2}, {1,0,4,2,3}, {1,0,4,3,2},
  201. {1,2,0,3,4}, {1,2,0,4,3}, {1,2,3,0,4}, {1,2,3,4,0}, {1,2,4,0,3},
  202. {1,2,4,3,0}, {1,3,0,2,4}, {1,3,0,4,2}, {1,3,2,0,4}, {1,3,2,4,0},
  203. {1,3,4,0,2}, {1,3,4,2,0}, {1,4,0,2,3}, {1,4,0,3,2}, {1,4,2,0,3},
  204. {1,4,2,3,0}, {1,4,3,0,2}, {1,4,3,2,0}, {2,0,1,3,4}, {2,0,1,4,3},
  205. {2,0,3,1,4}, {2,0,3,4,1}, {2,0,4,1,3}, {2,0,4,3,1}, {2,1,0,3,4},
  206. {2,1,0,4,3}, {2,1,3,0,4}, {2,1,3,4,0}, {2,1,4,0,3}, {2,1,4,3,0},
  207. {2,3,0,1,4}, {2,3,0,4,1}, {2,3,1,0,4}, {2,3,1,4,0}, {2,3,4,0,1},
  208. {2,3,4,1,0}, {2,4,0,1,3}, {2,4,0,3,1}, {2,4,1,0,3}, {2,4,1,3,0},
  209. {2,4,3,0,1}, {2,4,3,1,0}, {3,0,1,2,4}, {3,0,1,4,2}, {3,0,2,1,4},
  210. {3,0,2,4,1}, {3,0,4,1,2}, {3,0,4,2,1}, {3,1,0,2,4}, {3,1,0,4,2},
  211. {3,1,2,0,4}, {3,1,2,4,0}, {3,1,4,0,2}, {3,1,4,2,0}, {3,2,0,1,4},
  212. {3,2,0,4,1}, {3,2,1,0,4}, {3,2,1,4,0}, {3,2,4,0,1}, {3,2,4,1,0},
  213. {3,4,0,1,2}, {3,4,0,2,1}, {3,4,1,0,2}, {3,4,1,2,0}, {3,4,2,0,1},
  214. {3,4,2,1,0}, {4,0,1,2,3}, {4,0,1,3,2}, {4,0,2,1,3}, {4,0,2,3,1},
  215. {4,0,3,1,2}, {4,0,3,2,1}, {4,1,0,2,3}, {4,1,0,3,2}, {4,1,2,0,3},
  216. {4,1,2,3,0}, {4,1,3,0,2}, {4,1,3,2,0}, {4,2,0,1,3}, {4,2,0,3,1},
  217. {4,2,1,0,3}, {4,2,1,3,0}, {4,2,3,0,1}, {4,2,3,1,0}, {4,3,0,1,2},
  218. {4,3,0,2,1}, {4,3,1,0,2}, {4,3,1,2,0}, {4,3,2,0,1}, {4,3,2,1,0},
  219. };
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-9-18 21:15:44 | 显示全部楼层
原帖由 gxqcn 于 2008-9-18 07:34 发表 你可以试试由 shability 整理的《我整理的一些IQ题目合集》, 我记得里面并未完全解答。 还有 无心人 发的《数学奥林匹克升级题》,也非常有意思。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-9-18 21:20:27 | 显示全部楼层
我刚学函数,你们写的那些函数我都看不懂。给你们一个被我谢的机会,能不能找点资料帮我速成一下,我会感激不尽的。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-9-18 22:59:43 | 显示全部楼层
原帖由 zYr 于 2008-9-18 21:20 发表 我刚学函数,你们写的那些函数我都看不懂。给你们一个被我谢的机会,能不能找点资料帮我速成一下,我会感激不尽的。
慢慢来,罗马不是一天建成的
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-9-19 08:25:43 | 显示全部楼层
原帖由 gxqcn 于 2008-9-18 19:56 发表 刚才在 VC6.0 下编译出现了大量 error,就略作了下修改:
哦哦,我可是把那段程序拷贝了以后又重新拿去编译的,对比了一下文件同时分析了一下自己的编译环境,是我的VC的启动环境有点不一样(我的编译环境太复杂了,有VC6.0,VC9.0,MSC7.0,DriverStudio3.2,他们或之间会互相影响 )!引以为戒,感谢ing。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-9-19 08:37:24 | 显示全部楼层
估计主要是 include 的同名头文件的干扰吧。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-11-21 21:33 , Processed in 0.038256 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表