PHP编程解答2018刑侦推理试题

阅读 926
收藏 26
2018-03-08
原文链接:my.oschina.net

这是网上很火的2018刑侦推理试题,网上已经有人推理得出答案。

作为程序员当然想的是如何用编程得出答案.

php代码如下

<?php
$option = array('A','B','C','D');
function check_1($data){
	return true;
}

function check_2($data){
	$question = array('A'=>'C','B'=>'D','C'=>'A','D'=>'B');
	if($question[$data[2]] == $data[5]) return true;
	return false;
}

function check_3($data){
	$question = array('A'=>$data[3],'B'=>$data[6],'C'=>$data[2],'D'=>$data[4]);
	$answer = $question[$data[3]];
	unset($question[$data[3]]);
	if(!in_array($answer,$question)) return true;
	return false;
}

function check_4($data){
	$question = array('A'=>$data[1]==$data[5],'B'=>$data[2]==$data[7],'C'=>$data[1]==$data[9],'D'=>$data[6]==$data[10]);
	return $question[$data[4]];
}

function check_5($data){
	$question = array('A'=>$data[8],'B'=>$data[4],'C'=>$data[9],'D'=>$data[7]);
	if($question[$data[5]] == $data[5]) return true;
	return false;
}

function check_6($data){
	$question = array('A'=>$data[2]==$data[4] && $data[4]==$data[8],'B'=>$data[1]==$data[6] && $data[6]==$data[8],'C'=>$data[3]==$data[10] && $data[10]==$data[8],'D'=>$data[5]==$data[9] && $data[9]=$data[8]);
	return $question[$data[6]];	
}

function check_7($data){
	$question = array('A'=>'C','B'=>'B','C'=>'A','D'=>'D');
	$count = array_count_values($data);
	arsort($count);
	if(array_pop(array_keys($count))==$question[$data[7]]) return true;
	return false;
}

function check_8($data){
	$map = array('A'=>1,'B'=>2,'C'=>3,'D'=>4);
	$question = array('A'=>$data[7],'B'=>$data['5'],'C'=>$data['2'],'D'=>$data['10']);
	if(abs($map[$data[1]] - $map[$question[$data[8]]])>1) return true;
	return false;
}

function check_9($data){
	$question = array('A'=>$data[6],'B'=>$data[10],'C'=>$data[2],'D'=>$data[9]);
	if(($question[$data[9]] == $data[5])!=($data[1] == $data[6]))return true;
	return false;
	
}

function check_10($data){
	$question = array('A'=>3,'B'=>2,'C'=>4,'D'=>1);
	$count = array_count_values($data);
	arsort($count);
	$min = array_pop($count);
	asort($count);
	$max = array_pop($count);
	if($question[$data[10]] == $max-$min) return true;
	return false;
}

foreach($option as $v[1]){
	foreach($option as $v[2]){
		foreach($option as $v[3]){
			foreach($option as $v[4]){
				foreach($option as $v[5]){
					foreach($option as $v[6]){
						foreach($option as $v[7]){
							foreach($option as $v[8]){
								foreach($option as $v[9]){
									foreach($option as $v[10]){
										for($i=1;$i<=10;$i++){
											$answer[$i] = $v[$i];
										}
										for($i=1;$i<=10;$i++){
											$func = "check_".$i;
											if(!$func($answer)) break;
										}
										if($i == 11) {echo implode(' ',$answer)."\r\n";die;}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
?>

结果输出:B C A C A C D A B A

应该是正确答案,不过用的是最简单的暴力破解算法,不过计算机不就是干这事儿的么.

当然算法还可以想其他方法比如动态规划等等.

评论