2018刑侦推理题c++暴力破解

小鸡
阅读681 喜欢3 cpp 更新2018-5-14

最近看到很火的那个2018年刑侦推理题,很多程序员都用来编程暴力破解 感觉挺好玩,就自己也来用c++试一试 (正在学校机房上实验课的我,只能对着发白的codeblocks撸码)

先上结果

BCACACDABA     /0.086秒

代码

其实大家的方法都一样啦 就是穷举,判断。 因为写之前看到知乎上大佬把10个题目都放在一个判断语句里面判断了 觉得正合我意,便也只用一个if判断句


#include <iostream>
#include <math.h>
using namespace std;
bool test(int q[],int an[],int temp);

int main() {

int issue[10]={0};

for (int i = 0; i < 1048576; i++) {      //10题的答案有4^10=1048576种情况

for(int j=0;j<10;j++){               //给1~10题答案赋值

int k=i/pow(4,j);

issue[j]=k % 4;

}

int an[4] = { 0 }, min = 10, max = 0;

for (int j = 0; j < 10; j++) an[issue[j]]++;

for (int j = 0; j < 4; j++){

if(max<an[j])max=an[j];

if(min>an[j])min=an[j];

}

if(test(issue,an,max-min)) {            //调用判断函数暴力分析

for (int j = 0; j < 10; j++)

cout<<"第"<<j<<"题:"<<char(A + issue[j])<<endl;

break;

}

}

}

bool test(int q[10],int an[4],int temp)    //把题目转化成一堆没眼看的判断条件

{

if ((q[1] == 0 && q[4] == 2 ||q[1] == 1 && q[4] == 3 ||
q[1] == 2 && q[4] == 0 ||q[1] == 3 && q[4] == 1) &&

(q[2] == 0 && q[2] != q[5] && q[2] != q[1] && q[2] != q[3] ||

q[2] == 1 && q[5] != q[2] && q[5] != q[1] && q[5] != q[3] ||

q[2] == 2 && q[1] != q[2] && q[1] != q[5] && q[1] != q[3] ||

q[2] == 3 && q[3] != q[2] && q[3] != q[5] && q[3] != q[1]) &&

(q[3] == 0 && q[0] == q[4] ||q[3] == 1 && q[1] == q[6] ||

q[3] == 2 && q[0] == q[8] ||q[3] == 3 && q[5] == q[9]) &&

(q[4] == 0 && q[7] == 0 ||q[4] == 1 && q[3] == 1 ||

q[4] == 2 && q[8] == 2 ||q[4] == 3 && q[6] == 3) &&

(q[5] == 0 && q[1] == q[7] && q[3] == q[7] ||

q[5] == 1 && q[0] == q[7] && q[5] == q[7] ||

q[5] == 2 && q[2] == q[7] && q[9] == q[7] ||

q[5] == 3 && q[4] == q[7] && q[8] == q[7]) &&

(q[6] == 0 && an[2] < an[0] && an[2] < an[1] && an[2] < an[3] ||

q[6] == 1 && an[1] < an[0] && an[1] < an[2] && an[1] < an[3] ||

q[6] == 2 && an[0] < an[1] && an[0] < an[2] && an[0] < an[3] ||

q[6] == 3 && an[3] < an[0] && an[3] < an[1] && an[3] < an[2]) &&

(q[7] == 0 && (q[0] - q[6])*(q[0] - q[6]) != 1 ||

q[7] == 1 && (q[0] - q[4])*(q[0] - q[4]) != 1 ||

q[7] == 2 && (q[0] - q[1]) *(q[0] - q[1])!= 1 ||

q[7] == 3 && (q[0] - q[9]) *(q[0] - q[9])!= 1) &&

(q[8] == 0 && (q[0] == q[5]) != (q[5] == q[4]) ||

q[8] == 1 && (q[0] == q[5]) != (q[9] == q[4]) ||

q[8] == 2 && (q[0] == q[5]) != (q[1] == q[4]) ||

q[8] == 3 && (q[0] == q[5]) != (q[8] == q[4])) &&

(q[9] == 0 && temp == 3 ||q[9] == 1 && temp == 2 ||

q[9] == 2 && temp == 4 ||q[9] == 3 && temp == 1))

return true;

else return false;

}