hello
各位小伙伴
最近過的怎么樣
有沒有按時(shí)吃飯扔嵌,睡覺村缸,敲代碼
一定要照顧好自己呀~
不知道大家前段時(shí)間痢虹,有沒有在朋友圈中收到一份主儡,『2018的刑偵科目推理試題』娜娜偶然在朋友圈中看到過一次~
原圖奉上,沒有看過的小伙伴可以仔細(xì)的看一下哈~
我看到第一題的反應(yīng)是
題呢丰捷?你到給我題呀~連題都不給我~我怎么知道答案~
看到第二題我的腦袋已經(jīng)無法思考一片空白了~
這尼瑪是什么玩意兒臀玄,簡(jiǎn)直就是一個(gè)坑啊,然后我就放棄了健无。
但是最近我看到各路大神對(duì)這道題進(jìn)行了解答荣恐,我才意識(shí)到,原來看似這么不正經(jīng)的題其實(shí)是有很正經(jīng)的答案的~
娜娜~不禁想問累贤,這的死多少腦細(xì)胞才能做出這樣的推理題~看來我那900+多集的柯南是白看了~
python語言實(shí)現(xiàn)
#encoding: utf-8
# 《2018刑偵科推理試題》非窮舉的 Python 解法
# 需要先安裝約束解決庫 `pip3 install python-constraint`
# 使用 Python 3.5 編寫
from?constraint?import?*
problem = Problem()
# a1 - a10 表示第一題到第十題的答案變量叠穆,答案使用“1”表示“A”, “2”表示“B”,以此類推
vars = ["a1",?"a2",?"a3",?"a4",?"a5",?"a6",?"a7",?"a8",?"a9",?"a10"]
problem.addVariables(vars, [1,?2,?3,?4])
#第 2 題
def?a2_func(a2, a5):
return?(a2 ==?1?and?a5 ==?3)?or?(a2 ==?2?and?a5 ==?4)?or?(a2 ==?3?or?a5 ==?1)?or?(a2 ==?4?or?a5 ==?2)
problem.addConstraint(a2_func, ["a2",?"a5"])
#第 3 題
def?a3_func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10):
return?(a3 ==?1?and?a6 == a2 == a4 != a3)?or?(a3 ==?2?and?a3 == a2 == a4 != a6) \
or?(a3 ==?3?and?a3 == a6 == a4 != a2)?or?(a3 ==?4?and?a3 == a6 == a2 != a4)
problem.addConstraint(a3_func, vars)
#第 4 題
def?a4_func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10):
return?(a4 ==?1?and?a1 == a5)?or?(a4 ==?2?and?a2 == a7)?or?(a4 ==?3?and?a1 == a9)?or?(a4 ==?4?and?a6 == a10)
problem.addConstraint(a4_func, vars)
#第 5 題
def?a5_func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10):
return?(a5 == a8 ==?1)?or?(a5 == a4 ==?2)?or?(a5 == a9 ==?3)?or?(a5 == a7 ==?4)
problem.addConstraint(a5_func, vars)
#第 6 題
def?a6_func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10):
return?(a6 ==?1?and?a2 == a4 == a8)?or?(a6 ==?2?and?a1 == a6 == a8) \
?or?(a6 ==?3?and?a3 == a10 == a8)?or?(a6 ==?4?and?a5 == a9 == a8)
problem.addConstraint(a6_func, vars)
#第 7 題
def?a7_func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10):
all_answers = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]
counter = [0,?0,?0,?0]
for?a?in?all_answers:?
counter[a -?1] +=?1
imin = counter.index(min(counter)) +?1
n = len(set(counter))
# 這題為“此10題種被選中的選項(xiàng)最少的為臼膏,ABCD選擇硼被,說明 ABCD 的數(shù)量個(gè)不相同”
return?n ==?4?and?((a7 ==?1?and?imin ==?3)?or?(a7 ==?2?and?imin ==?2)?or?(a7 ==?3?and?imin ==?1)?or?(a7 ==?4?and?imin ==?4))
problem.addConstraint(a7_func, vars)
#第 8 題
def?a8_func(a8, a7, a5, a2, a10, a1):
adj =?lambda?x: abs(a1 - x) !=?1
return?(a8 ==?1?and?adj(a7))?or?(a8 ==?2?and?adj(a5))?or?(a8 ==?3?and?adj(a2))?or?(a8 ==?4?and?adj(a10))
problem.addConstraint(a8_func, ["a8",?"a7",?"a5",?"a2",?"a10",?"a1"])
#第 9 題
def?a9_func(a1, a2, a5, a6, a9, a10):
cond1 = a1 == a6
cond =?lambda?x: cond1 != (x == a5)
return?(a9 ==?1?and?cond(a6))?or?(a9 ==?2?and?cond(a10))?or?(a9 ==?3?and?cond(a2))?or?(a9 ==?4?and?cond(a9))
problem.addConstraint(a9_func, ["a1",?"a2",?"a5",?"a6",?"a9",?"a10"])
#第 10 題
def?a10_func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10):
all_answers = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]
counter = [0,?0,?0,?0]
for?a?in?all_answers:?
counter[a -?1] +=?1
v = abs(max(counter) - min(counter))
return?(a10 ==?1?and?v ==?3)?or?(a10 ==?2?and?v ==?2)?or?(a10 ==?3?and?v ==?4)?or?(a10 ==?4?and?v ==?1)
problem.addConstraint(a10_func, vars)
# 約束設(shè)置完畢,開始求解
solutions = problem.getSolutions()
print("可能的解決數(shù)量:{0}".format(len(solutions)))
chars = ['A',?'B',?'C',?'D']
for?si?in?range(0, len(solutions)):
print("\n---------- 第 {0} 組結(jié)果 ----------".format(si +?1))
s = solutions[si]
for?i?in?range(1,?11):
key =?"a"?+ str(i)
answer = chars[s[key] -?1]
print("第 {0} 題答案:{1}".format(i, answer))
運(yùn)行結(jié)果:
c++語言實(shí)現(xiàn)
C語言實(shí)現(xiàn):
一上來就是兩發(fā)渗磅,下面代碼是精簡(jiǎn)后的代碼嚷硫。
#include
#define?JUDGE(i, JA, JB, JC, JD) \
if?(c[i] ==?0?&& (JA))?continue; \
if?(c[i] ==?1?&& (JB))?continue; \
if?(c[i] ==?2?&& (JC))?continue; \
if?(c[i] ==?3?&& (JD))?continue;?
void?main()?{
int?c[10], n[4], dn, sn, dc, sc;?
for?(int?k =?0; k < (1?<<?20); ++k) {
n[0] = n[1] = n[2] = n[3] =?0;
for?(int?i =?0; i <?10; ++i) {
c[i] = ((k >>?2*i) &?3);
++n[c[i]];
}
dn =?0, sn =?10;
for?(int?i =?0; i <?4; ++i) {
if?(dn < n[i]) {dn = n[i]; dc = i;}
if?(sn > n[i]) {sn = n[i]; sc = i;}
}
JUDGE(1, c[4] !=?2, c[4] !=?3, c[4] !=?0, c[4] !=?1);
JUDGE(2, c[2] == c[5] || c[2] == c[1] || c[2] == c[3],?
c[5] == c[2] || c[5] == c[1] || c[5] == c[3],
c[1] == c[2] || c[1] == c[5] || c[1] == c[3],?
c[3] == c[2] || c[3] == c[1] || c[3] == c[5]);
JUDGE(3, c[0] != c[4], c[1] != c[6], c[0] != c[8], c[5] != c[9]);
JUDGE(4, c[4] != c[7], c[4] != c[3], c[4] != c[8], c[4] != c[6]);
JUDGE(5, c[7] != c[1] || c[7] != c[3], c[7] != c[0] || c[7] != c[5],?
c[7] != c[2] || c[7] != c[9], c[7] != c[4] || c[7] != c[8]);
JUDGE(6, sc !=?2, sc !=?1, sc !=?0, sc !=?3);
JUDGE(7, c[0] - c[6] ==?1?|| c[0] - c[6] ==?-1,?
c[0] - c[4] ==?1?|| c[0] - c[4] ==?-1,?
c[0] - c[1] ==?1?|| c[0] - c[1] ==?-1,?
c[0] - c[9] ==?1?|| c[0] - c[9] ==?-1);
if?(c[0] == c[5]) {
JUDGE(8, c[4] == c[5], c[4] == c[9], c[4] == c[1], c[4] == c[8]);
}?else?{
JUDGE(8, c[4] != c[5], c[4] != c[9], c[4] != c[1], c[4] != c[8]);
}
JUDGE(9, dn - sn !=?3, dn - sn !=?2, dn - sn !=?4, dn - sn !=?1);
printf("1:%c, 2:%c, 3:%c, 4:%c, 5:%c, 6:%c, 7:%c, 8:%c, 9:%c, 10:%c",
'A'+c[0],?'A'+c[1],?'A'+c[2],?'A'+c[3],?'A'+c[4],?
'A'+c[5],?'A'+c[6],?'A'+c[7],?'A'+c[8],?'A'+c[9]);
}
}
Java語言實(shí)現(xiàn)
public?static?void?answer(){
// 用 1234 分別對(duì)應(yīng) ABCD, 計(jì)算方便
int[] answers = {1,2,3,4};
// 群舉答案
for?(int?q1 : answers) {
for?(int?q2 : answers) {
for?(int?q3 : answers) {
for?(int?q4 : answers) {
for?(int?q5 : answers) {
for?(int?q6 : answers) {
for?(int?q7 : answers) {
for?(int?q8 : answers) {
for?(int?q9 : answers) {
for?(int?q10 : answers) {
int[] questions =?new?int[10];
questions[0] = q1;
questions[1] = q2;
questions[2] = q3;
questions[3] = q4;
questions[4] = q5;
questions[5] = q6;
questions[6] = q7;
questions[7] = q8;
questions[8] = q9;
questions[9] = q10;
if?(isEnd(questions)){
// 遍歷輸出符合條件的答案
for?(int?i =?0?; i <?10; i++){
System.out.println((i+1) +?":"?+ questions[i]);
}
}
}
}
}
}
}
}
}
}
}
}
}
/**
* 判斷每個(gè)答案是否符合題意
* 為了方便 questions 數(shù)組中從 0 開始,
* 題目比數(shù)組角標(biāo)多 1(不要問為什么, 奏是這么開)
* 比如 question[0] 的值表示第 1 題答案
**/
static?boolean?isEnd(int[] questions){
// 第二題, 第 5 題的答案是
switch?(questions[4]){
case?1:
// 如果第 5 題答案是 A, 判斷第 2 題答案是不是 C 不是返回 false, 是繼續(xù)
if(questions[1] !=?3)
return?false;
break;
case?2:
// 原理同上
if(questions[1] !=?4)
return?false;
break;
case?3:
// 原理同上
if(questions[1] !=?1)
return?false;
break;
case?4:
// 原理同上
if(questions[1] !=?2)
return?false;
break;
}
// 第 3 題, 以下選項(xiàng)中哪一題的答案與其他三項(xiàng)不同
switch?(questions[2]){
case?1:
if(!(questions[2]!=questions[5] && questions[5]==questions[1] && questions[1] == questions[3]))
return?false;
break;
case?2:
if(!(questions[5]!=questions[2] && questions[2]==questions[1] && questions[1] == questions[3]))
return?false;
break;
case?3:
if(!(questions[1]!=questions[5] && questions[2]==questions[5] && questions[5] == questions[3]))
return?false;
break;
case?4:
if(!(questions[3]!=questions[5] && questions[5]==questions[1] && questions[1] == questions[2]))
return?false;
break;
}
// 第 4 題, 以下選項(xiàng)中那兩題的答案相同
switch?(questions[3]){
case?1:{
// 判斷第 1 題與第 5 題答案是否相同
if?(questions[0] != questions[4]){
return?false;
}
break;
}
case?2:{
// 原理同上
if?(questions[1] != questions[6]){
return?false;
}
break;
}
case?3:{
// 原理同上
if?(questions[0] != questions[8]){
return?false;
}
break;
}
case?4:{
// 原理同上
if?(questions[5] != questions[9]){
return?false;
}
break;
}
}
// 第 5 題, 以下選項(xiàng)中哪一題的答案與本題相同
switch?(questions[4]){
case?1:
// 判斷第 8 題答案是否是 A
if?(questions[7] !=?1)
return?false;
break;
case?2:
// 原理同上
if(questions[3] !=?2)
return?false;
break;
case?3:
// 原理同上
if?(questions[8] !=?3)
return?false;
case?4:
// 原理同上
if?(questions[6] !=?4)
return?false;
break;
}
// 第 6 題, 以下選項(xiàng)中哪兩題的答案與第 8 題相同
switch?(questions[5]){
case?1:
// 判斷第 14 題答案是否與第 8 題答案相同
if(questions[1] != questions[7] || questions[4] != questions[7])
return?false;
break;
case?2:
// 原理同上
if(questions[0] != questions[7] || questions[5] != questions[7])
return?false;
break;
case?3:
// 原理同上
if(questions[2] != questions[7] || questions[9] != questions[7])
return?false;
break;
case?4:
// 原理同上
if(questions[4] != questions[7] || questions[8] != questions[7])
return?false;
break;
}
// 由于第 710 題問題是同類型的, 所以一塊計(jì)算 start
int[] check10 =?new?int[5];
// 把每個(gè)題的答案 (1234) 作為新數(shù)組下表, value++ 計(jì)算出現(xiàn)次數(shù)
for?(int?i=0;i < questions.length;i++){
check10[questions[i]]++;
}
// 出現(xiàn)最少與最多選項(xiàng)的次數(shù)初始化為 A 的次數(shù)
int?low = check10[1];
int?longer = check10[1];
// 出現(xiàn)最少的選項(xiàng), 初始化為 A
int?lowA =?1;
// 最少與最多次數(shù)的選項(xiàng)相關(guān)計(jì)算
for?(int?i=1;i<5;i++) {
if(check10[i] >0?&& check10[i] < low){
low = check10[i];
lowA = i;
}
if?(check10[i] > longer){
longer = check10[i];
}
}
// 第 7 題, 在此十道題中, 被選中次數(shù)最少的選項(xiàng)字母為
switch?(questions[6]){
case?1:
// 判斷才出現(xiàn)最少的字母是否為 C
if?(lowA !=?3)
return?false;
break;
case?2:
// 原理同上
if?(lowA !=?2)
return?false;
break;
case?3:
// 原理同上
if?(lowA !=?1)
return?false;
break;
case?4:
// 原理同上
if?(lowA !=?4)
return?false;
break;
}
// 第 10 題, 在此 10 道題中, ABCD 四個(gè)字母出現(xiàn)次數(shù)最多與最少者的差為
// 最多次數(shù)與最少次數(shù)的差值
int?t = longer-low;
switch?(questions[9]){
case?1:
// 判斷差值是否為 3
if?(t !=?3)
return?false;
break;
case?2:
// 原理同上
if?(t !=?2)
return?false;
break;
case?3:
// 原理同上
if?(t !=?4)
return?false;
break;
case?4:
// 原理同上
if?(t !=?1)
return?false;
break;
}
// 第 710 題校驗(yàn) end
// 第 8 題, 以下選項(xiàng)中哪一題的答案與第 1 題的答案在字母中不相鄰
switch?(questions[7]) {
case?1:
// 判斷第 7 題與第一題答案差值絕對(duì)是是否為 1
if?(Math.abs(questions[6] - questions[0]) ==?1)
return?false;
break;
case?2:
// 原理同上
if?(Math.abs(questions[4] - questions[0]) ==?1)
return?false;
break;
case?3:
// 原理同上
if?(Math.abs(questions[1] - questions[0]) ==?1)
return?false;
break;
case?4:
// 原理同上
if?(Math.abs(questions[9] - questions[0]) ==?1)
return?false;
break;
}
// 第 9 題, 已知第 1 題與第 6 題的答案相同與第 X 題與第 5 題的答案相同的真假性相反, 那么 X 為
// 判斷第 1 題與第 6 題的答案是否相同
boolean isOne = questions[0]==questions[5]?true:false;
switch?(questions[8]){
case?1:
if(isOne){
// 第 1 題與第 6 題相同, 第 6 題與第 5 題答案相同返回 false;
if?(questions[5] == questions[4])
return?false;
}else?{
// 第 1 題與第 6 題不相同, 第 6 題與第 5 題答案不相同返回 false;
if?(questions[5] != questions[4])
return?false;
}
break;
case?2:
// 原理同上
if(isOne){
if?(questions[9] == questions[4])
return?false;
}else?{
if?(questions[9] != questions[4])
return?false;
}
break;
case?3:
// 原理同上
if(isOne){
if?(questions[1] == questions[4])
return?false;
}else?{
if?(questions[1] != questions[4])
return?false;
}
break;
case?4:
// 原理同上
if(isOne){
if?(questions[8] == questions[4])
return?false;
}else?{
if?(questions[8] != questions[4])
return?false;
}
break;
}
return?true;
}
public?static?void?main(String[] args)?{
answer();
}
看到上面的答案之后检访,娜娜躲在角落了瑟瑟發(fā)抖,大牛仔掸,不就是膝蓋嗎脆贵?我給還不行嗎?
然后我看到了下面這條信息起暮,這一是道杭州學(xué)軍中學(xué)的推理社的招新題~招新題~
然后娜娜開始研究怎么把這道題解出來~
此時(shí)此刻娜娜
娜娜的心情就和外面的天氣一樣
當(dāng)我以為這題沒有答案的時(shí)候
有人做出了答案
當(dāng)我以為做答案的都是大牛的時(shí)候
有人說這是入門級(jí)的題
當(dāng)我想著別人能做我也能做到的時(shí)候
發(fā)現(xiàn)自己竟然做不出來
各位小伙伴
開往幼兒園的班車就要發(fā)車了
沒做出來的小伙伴請(qǐng)上車
和娜娜一起回去學(xué)習(xí)
不要擔(dān)心
我一定會(huì)回來的~~~