一道谜一样的推理题
今天,hyb发来了一道开玩笑一般的推理题,这里分享一下
········
我决定发扬优秀的程序猿精神
于是·········
var a:array[1..10]of char;
sum:array['A'..'D']of longint;
function check:boolean;
var k:boolean;
max,min:longint;
maxx,minn:char;
j:char;
begin
max:=-1;
min:=maxlongint;
for j:='A' to 'D' do
begin
if sum[j]>max then
begin
max:=sum[j];
maxx:=j;
end;
if sum[j]<min then
begin
min:=sum[j];
minn:=j;
end;
end;
k:=true;
check:=true;
case a[2] of
'A':if a[5]='C' then k:=false;
'B':if a[5]='D' then k:=false;
'C':if a[5]='A' then k:=false;
'D':if a[5]='B' then k:=false;
end;
if k then exit(false);
k:=true;
case a[3] of
'A':if (a[6]=a[2])and(a[6]=a[4])and(a[3]<>a[6]) then k:=false;
'B':if (a[3]=a[2])and(a[3]=a[4])and(a[3]<>a[6]) then k:=false;
'C':if (a[6]=a[3])and(a[6]=a[4])and(a[2]<>a[6]) then k:=false;
'D':if (a[6]=a[2])and(a[6]=a[3])and(a[4]<>a[6]) then k:=false;
end;
if k then exit(false);
k:=true;
case a[4] of
'A':if a[1]=a[5] then k:=false;
'B':if a[2]=a[7] then k:=false;
'C':if a[1]=a[9] then k:=false;
'D':if a[6]=a[10] then k:=false;
end;
if k then exit(false);
k:=true;
case a[5] of
'A':if a[8]=a[5] then k:=false;
'B':if a[4]=a[5] then k:=false;
'C':if a[9]=a[5] then k:=false;
'D':if a[7]=a[5] then k:=false;
end;
if k then exit(false);
k:=true;
case a[6] of
'A':if (a[8]=a[2])and(a[8]=a[4]) then k:=false;
'B':if (a[8]=a[1])and(a[8]=a[6]) then k:=false;
'C':if (a[8]=a[3])and(a[8]=a[10]) then k:=false;
'D':if (a[8]=a[5])and(a[8]=a[9]) then k:=false;
end;
if k then exit(false);
k:=true;
case a[7] of
'A':if min=sum['C'] then k:=false;
'B':if min=sum['B'] then k:=false;
'C':if min=sum['A'] then k:=false;
'D':if min=sum['D'] then k:=false;
end;
if k then exit(false);
k:=true;
case a[8] of
'A':if abs(ord(a[7])-ord(a[1]))=1 then exit(false);
'B':if abs(ord(a[5])-ord(a[1]))=1 then exit(false);
'C':if abs(ord(a[2])-ord(a[1]))=1 then exit(false);
'D':if abs(ord(a[10])-ord(a[1]))=1 then exit(false);
end;
k:=true;
case a[9] of
'A':if (a[1]=a[6])<>(a[6]=a[5]) then k:=false;
'B':if (a[1]=a[6])<>(a[10]=a[5]) then k:=false;
'C':if (a[1]=a[6])<>(a[2]=a[5]) then k:=false;
'D':if (a[1]=a[6])<>(a[9]=a[5]) then k:=false;
end;
if k then exit(false);
k:=true;
case a[10] of
'A':if (max-min)=3 then k:=false;
'B':if (max-min)=2 then k:=false;
'C':if (max-min)=4 then k:=false;
'D':if (max-min)=1 then k:=false;
end;
if k then exit(false);
exit(true);
end;
procedure dfs(l:longint);
var c:char;
i:longint;
begin
if l=11 then
begin
if check then
begin
for i:=1 to 10 do
write(a[i],' ');
writeln;
end;
exit;
end;
for c:='A' to 'D' do
begin
a[l]:=c;
inc(sum[c]);
dfs(l+1);
dec(sum[c]);
end;
end;
begin
dfs(1);
end.
答案是B C A C A C D A B A