写了个找凶手的题目 不知道错在那里???

csdn小虫 2008-01-08 05:41:29
就放一个按钮和edit
线索
1:b[1] b[2]至少一个凶手
2:b[1] b[4]不可能同时作案
3:b[2] b[3]要么同时作案 要么都不作案
4:b[3] b[4]只有一个作案
5:b[4]没做 b[5]也不可能做
6:b[1] b[5] b[6]有两个人作案


procedure TfrmMain.Button1Click(Sender: TObject);
var
a:array[1..6] of boolean;
flag:array[0..1] of boolean;
count:integer; //用来记录符合条件的数目
i,j,k,l,m,n:integer;

procedure judge(b:array of boolean);
begin
if (b[1] or b[2])=false then //b[1] b[2]至少一个凶手
begin
count:=0;
exit;
end
else
count:=count+1;

if (b[1] and b[4])=true then //b[1] b[4]不可能同时作案
begin
count:=0;
exit;
end
else
count:=count+1;

if (b[2] xor b[3])=true then //b[2] b[3]要么同时作案 要么都不作案
begin
count:=0;
exit;
end
else
count:=count+1;

if (b[3] xor b[4])=false then //b[3] b[4]只有一个作案
begin
count:=0;
exit;
end
else
count:=count+1;

if (not b[4] and b[5])=true then //b[4]没做 b[5]也不可能做
begin
count:=0;
exit;
end
else
count:=count+1;

if ((b[1] and b[5] and b[6])=true) or ((b[1] or b[5] or b[6])=false) or ((b[1] xor b[5] xor b[6])=true) then
//b[1] b[5] b[6]有两个人作案
begin
count:=0;
exit;
end
else
count:=count+1;
end;

begin
flag[0]:=false;
flag[1]:=true;
count:=0;
edit1.Text:='';
for i:=0 to 1 do
for j:=0 to 1 do
for k:=0 to 1 do
for l:=0 to 1 do
for m:=0 to 1 do
for n:=0 to 1 do
begin
a[1]:=flag[i];
a[2]:=flag[j];
a[3]:=flag[k];
a[4]:=flag[l];
a[5]:=flag[m];
a[6]:=flag[n];
judge(a);
if count=6 then
begin
if a[1]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[2]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[3]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[4]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[5]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[6]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
end;
end;
end;

在edit中显示的是空的
不知道错在哪里?????
...全文
199 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn小虫 2008-01-18
  • 打赏
  • 举报
回复
问题已经都解决了

谢谢大家的热心帮忙
csdn小虫 2008-01-15
  • 打赏
  • 举报
回复
今天 我发现我上面的循环 怎么到了找到符合条件的情况之后
后面就不执行了

比如说我到了第100种情况时 符合了条件
但是后面的情况它不执行了
到底怎么回事?????

因为我用了progressbar跟踪所有可能情况
但是到了找到符合条件时候 progressbar就不动了

???????
哪里出错了???????
  • 打赏
  • 举报
回复
总共就64种情况,你能找出第100种来?:)
csdn小虫 2008-01-09
  • 打赏
  • 举报
回复
我先前也注意了那个问题
我加上了count清零的
也是不行的
coffeemay 2008-01-09
  • 打赏
  • 举报
回复
每次 judge 都需要对 count 清0

所以judge函数第一行令count:=0

最里面一次循环judge(a); 前加count:=0;
csdn小虫 2008-01-09
  • 打赏
  • 举报
回复
循环也没有错

大哥请指点

怎么不行呀????

急!!!!
csdn小虫 2008-01-09
  • 打赏
  • 举报
回复
你说的两种情况都不行
b[2] b[3]要么同时要么都不做 你就不符合

我想问的是 我那个怎么不行??

for i:=0 to 1 do
begin
for j:=0 to 1 do
begin
for k:=0 to 1 do
begin
for l:=0 to 1 do
begin
for m:=0 to 1 do
begin
for n:=0 to 1 do
begin
a[1]:=flag[i];
a[2]:=flag[j];
a[3]:=flag[k];
a[4]:=flag[l];
a[5]:=flag[m];
a[6]:=flag[n];
judge(a);
if count=6 then
begin
if a[1]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[2]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[3]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[4]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[5]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[6]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
end
else
break;
end;
end;
end;
end;
end;
end;


感觉 for循环中有错误
但是不知道错在哪里???
dunzimu 2008-01-09
  • 打赏
  • 举报
回复
都在玩逻辑问题啊,这好像都是些绕圈子的问题啊,问一下,楼主做的啥系统啊。
  • 打赏
  • 举报
回复
自食其言,还是写了一个程序,主要是看到你们的代码太笨拙。:)

{$APPTYPE CONSOLE}
program FindMurderers;
var
m: array[1..6] of boolean;
i, j: integer;
{ main }
begin
for i := 0 to 2 * 2 * 2 * 2 * 2 * 2 - 1 do
begin
for j := 1 to 6 do
m[j] := boolean(i shr (j - 1) and 1);
if not (m[1] or m[2]) then continue;
if m[1] and m[4] then continue;
if m[2] xor m[3] then continue;
if not (m[3] xor m[4]) then continue;
if not m[4] and m[5] then continue;
if not ((m[1] and m[5] and not m[6]) or
(not m[1] and m[5] and m[6]) or
(m[1] and not m[5] and m[6])) then continue;
for j := 1 to 6 do
if m[j] then
writeln(j, ' is murderer')
else
writeln(j, ' isn''t murderer');
break;
end;
end.
  • 打赏
  • 举报
回复
b[4]没做 b[5]也不可能做

这个理解上有歧义,可以理解成“b[4]、b[5]都没做”,也可以理解成“如果b[4]没做,那么b[5]也没做”。
不过怎么理解都不影响结果。
王集鹄 2008-01-09
  • 打赏
  • 举报
回复
楼主的判断写得没有问题,顺便改进了一下。
var
a: array[1..6] of Boolean;
i, j, k, l, m, n: Boolean;

function judge: Boolean;
begin
Result := False;
if not (a[1] or a[2]) then Exit; //a[1] a[2]至少一个凶手
if a[1] and a[4] then Exit; //a[1] a[4]不可能同时作案
if a[2] xor a[3] then Exit; //a[2] a[3]要么同时作案 要么都不作案
if not (a[3] xor a[4]) then Exit; //a[3] a[4]只有一个作案
if not a[4] and a[5] then Exit; //a[4]没做 a[5]也不可能做
if ((a[1] and a[5] and a[6])) or
(not (a[1] or a[5] or a[6])) or
(a[1] xor a[5] xor a[6]) then Exit;
Result := True;
end;

begin
Edit1.Clear;
for i := False to True do
for j := False to True do
for k := False to True do
for l := False to True do
for m := False to True do
for n := False to True do
begin
a[1] := i;
a[2] := j;
a[3] := K;
a[4] := l;
a[5] := m;
a[6] := n;
if judge then
begin
Edit1.Text := Edit1.Text + IntToStr(Ord(A[1]));
Edit1.Text := Edit1.Text + IntToStr(Ord(A[2]));
Edit1.Text := Edit1.Text + IntToStr(Ord(A[3]));
Edit1.Text := Edit1.Text + IntToStr(Ord(A[4]));
Edit1.Text := Edit1.Text + IntToStr(Ord(A[5]));
Edit1.Text := Edit1.Text + IntToStr(Ord(A[6]));
end;
end;
end;


楼主的问题不是由于条件判断错误,而是由于使用动态数组参数后没有正确使用下标导致!

var
a: array[1..6] of Boolean;
procedure test(b: array of Boolean);
begin
ShowMessage(Format('%d,%d', [Low(b), High(b)])); // 测试的结果是0,5。这样看来,动态数组参数并非原来的[1..6]
end;
begin
test(a);
end;
csdn小虫 2008-01-09
  • 打赏
  • 举报
回复
谢谢大家的知道
特别感谢coffeemay
就是你说的问题 动态数组做参数 下标是从0开始的

问题解决了

王集鹄 2008-01-09
  • 打赏
  • 举报
回复
结果一个:111001
  function Check: Boolean;
begin
Result := (Ord(b[1]) + Ord(b[2]) >= 1) and //1:b[1] b[2]至少一个凶手
(not (b[1] and b[4])) and //2:b[1] b[4]不可能同时作案
(b[2] = b[3]) and //3:b[2] b[3]要么同时作案 要么都不作案
(Ord(b[3]) + Ord(b[4]) = 1) and //4:b[3] b[4]只有一个作案
(b[4] or (not b[4] and not b[5])) and //5:b[4] 没做 b[5]也不可能做
(Ord(b[1]) + Ord(b[5]) + Ord(b[6]) = 2); //6:b[1] b[5] b[6]有两个人作案
end;


修改了一下。

3:b[2] b[3]要么同时作案 要么都不作案
b[2] = b[3]

这条有点不好理解
5:b[4] 没做 b[5]也不可能做
这里含有b[4]做了的情况,那么可以不管b[5]是否做了
只有b[4]没做b[5]才没做。
b[4] or (not b[4] and not b[5])
csdn小虫 2008-01-09
  • 打赏
  • 举报
回复
还有就是 等于6的情况 我把那个真假值到 judge中判断了一下
有的不符合 那为什么还会等于6
  • 打赏
  • 举报
回复
1236,有时间喝杯茶,养养眼睛,写什么程序。:)
coffeemay 2008-01-09
  • 打赏
  • 举报
回复
if count=6 then
begin
if a[1]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[2]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[3]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[4]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[5]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
if a[6]=false then edit1.Text:=edit1.Text+'0' else edit1.Text:=edit1.Text+'1';
end
else
break;


你这样输出的结果是倒过来的
另外后面这个break是不能加的
coffeemay 2008-01-09
  • 打赏
  • 举报
回复
procedure judge(b:array of boolean);

动态数组作为形参下标从0开始计数而不是1,所以b是 array[0..5] of boolean;
csdn小虫 2008-01-09
  • 打赏
  • 举报
回复
并且我改了了一下 直接输出count的值 有count=6的情况
但是原程序中为什么 edit中没显示当count=6时 数组的取值情况????
王集鹄 2008-01-08
  • 打赏
  • 举报
回复
计算结果有两种情况
101010
010111

var
b: array[1..6] of Boolean;

function Check: Boolean;
begin
Result := (Ord(b[1]) + Ord(b[2]) >= 1) and //1:b[1] b[2]至少一个凶手
(not (b[1] and b[4])) and //2:b[1] b[4]不可能同时作案
(b[2] <> b[3]) and //3:b[2] b[3]要么同时作案 要么都不作案
(Ord(b[3]) + Ord(b[4]) = 1) and //4:b[3] b[4]只有一个作案
(not (not b[4] and not b[5])) and //5:b[4] 没做 b[5]也不可能做
(Ord(b[1]) + Ord(b[5]) + Ord(b[6]) = 2); //6:b[1] b[5] b[6]有两个人作案
end;

var
I, J: Integer;
S: string;
begin
Edit1.Clear;
FillChar(b[1], SizeOf(b), 0);
//00000000 -> 00111111// 二进制
for I := 0 to $3F do
begin
for J := 0 to 5 do
b[J + 1] := I and (1 shl J) <> 0;
if Check then
begin
S := '';
for J := 0 to 5 do
S := S + IntToStr(Ord(b[J + 1]));
Edit1.Text := S;
end;
end;
end;

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧