您的位置:首页 > 其它

2018刑侦科推理试题 穷举解法

2018-03-06 17:41 190 查看

2018刑侦科推理试题 穷举解法

前几天看见一道网红题

如下图



看起来很有意思。

在上课的时候拿起笔推了一下,未果。。。

于是怒发冲冠,不行咱就穷举吧

以下是我的穷举代码,答案是BCACACDABA

/**
* Create by XiaoLaZhua
*/

//定义题目
//选项工厂
function answerFactory(...args){
let answerSet = ['A','B','C','D']
let result = {};
for(let index in args){
result[answerSet[index]] = args[index]
}
return result;
}

//定义问题
const Questions = (() => {
return {
question2:answerFactory('C','D','A','B'),
question3:answerFactory('3','6','2','4'),
question4:answerFactory(['1','5'],['2','7'],['1','9'],['6','10']),
question5:answerFactory('8','4','9','7'),
question6:answerFactory(['2','4'],['1','6'],['3','10'],['5','9']),
question7:answerFactory('C','B','A','D'),
question8:answerFactory('7','5','2','10'),
question9:answerFactory('6','10','2','9'),
question10:answerFactory('3','2','4','1')
}
})();

//产生答案序列 对比答案
function createAnswer(){
let answerObj
for(let i = 0;i < Math.pow(4,10);i++){
answerObj = translateAnswer(i.toString(4).padStart(10,0))
//对比答案
console.log(`being in exhaustive... (${i})`);
if(valider(answerObj)) {
console.log(JSON.stringify(answerObj))
break;
};
}
}

//转换答案对象
function translateAnswer(str){
let answerSet = ["A","B","C","D"]
let answer = {}
for(let index in str){
answer[parseInt(index) + 1] = answerSet[str[index]]
}
return answer;
}

//验证对象
function valider(answer){
let answerSet = ["A","B","C","D"];
let more,less;
let obj = {
question2:() => Questions.question2[answer["2"]] == answer["5"],
question3:() => {
let result = [];
for(let val of answerSet){
if(val != answer["3"]) result.push(answer[Questions.question3[val]]);
}
return (result[0] == result[1] && result[1] == result[2] && result[0] != answer[Questions.question3[answer['3']]]);
},
question4:() => {
let arr = Questions.question4[answer["4"]]
return answer[arr[0]] == answer[arr[1]]
},
question5:() => answer[Questions.question5[answer["5"]]] == answer["5"],
question6:() => {
let arr = Questions.question6[answer["6"]]
return answer[arr[0]] == answer[arr[1]] && answer[arr[1]] == answer["8"]
},
question7:() => {
let str = ''
for(let i = 1;i <= 10;i++){
str += answer[i + '']
}
let arr = [];
for(let val of str.split('')){
arr.push({count:str.match(new RegExp(val,'g')).length,answer:val});
}
arr.sort((a,b) => a.count - b.count);
less = arr[0].count;
more = arr[9].count;
return arr[0].answer == answer['7']
},
question8:() => {
let aIndex = answerSet.indexOf(answer[Questions.question8[answer['8']]])
let index = answerSet.indexOf(answer['1'])
return aIndex != index && aIndex != index + 1 && aIndex != index -1
},
question9:() => {
let bool1 = answer['1'] == answer['6']
let bool2 = answer[Questions.question9[answer['9']]] == answer['5']
return (bool1 || bool2) && !(bool1 && bool2)
},
question10:() => (more - less) == parseInt(Questions.question10[answer['10']])
}

return obj.question2() && obj.question3() && obj.question4() && obj.question5() && obj.question6() && obj.question7() && obj.question8() && obj.question9() && obj.question10();
};

createAnswer();


附上结果



把本不是线性的推理过程变为线性的,这可能是程序员解答这道题的最大的优势了吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: