主题:问个菜鸟问题
就是倒水问题,3个杯子,知道容量,容量最大的杯子水装满,目标水量知道
要求找出次数最少的倒法
下面是我写出来的程序~~~运行之后老是提示‘exited with exitcode =2'
我是用free pascal的
帮帮我吧~~~我困扰了好久
Program water;
Var a:array [1..4,1..3000] of integer;
r:array [1..3] of integer;
i,j,re:integer;
f1,f2:text;
Function cc(w:integer):boolean;
var f:boolean;
k:integer;
begin
f:=true;
for k:=1 to 3 do
if a[k,w]>r[k] then
f:=false;
cc:=f;
end;
Function pd(w:integer):boolean;
var k,s:integer;
f:boolean;
begin
f:=true;
for k:=1 to w-1 do
if a[1,k]=a[1,w] then
if a[2,k]=a[2,w] then
if a[3,k]=a[3,w] then begin
f:=false;
k:=w;
end;
pd:=f;
end;
Function search(w:integer):boolean;
var f:boolean;
k:integer;
begin
f:=false;
for k:=1 to 3 do
if a[k,w]=re then f:=true;
search:=f;
end;
Procedure print(var w:integer);
var b:array [1..1000] of integer;
k,s:integer;
begin
k:=1;
b[k]:=w;
repeat
w:=a[4,w];
k:=k+1;
b[k]:=w;
until w=1;
while k<>0 do begin
s:=b[k];
k:=k-1;
writeln(f2,a[1,s],a[2,s],a[3,s]);
end;
close(f1);
close(f2);
halt;
end;
Procedure splite;
var i,j,k,s:integer;
begin
i:=1;
j:=1;
repeat
for k:=1 to 3 do
if a[k,i]<>0 then
for s:=1 to 3 do
if s<>k then begin
inc(j);
a[s,j]:=a[s,i]+a[k,i];
a[6-k-s,j]:=a[6-k-s,i];
if cc(j)=false then begin
a[k,j]:=a[s,j]-r[s];
a[s,j]:=r[s];
end;
if pd(j)=false then begin
j:=j-1;
if search(j) then
print(j);
end;
end;
inc(i);
until j>=3000;
end;
Begin
assign(f1,'water.in');
reset(f1);
assign(f2,'water.out');
rewrite(f2);
read(f1,r[1],r[2],r[3],re);
for i:=1 to 3000 do
for j:=1 to 4 do
a[j,i]:=0;
a[3,1]:=r[3];
splite;
End.
要求找出次数最少的倒法
下面是我写出来的程序~~~运行之后老是提示‘exited with exitcode =2'
我是用free pascal的
帮帮我吧~~~我困扰了好久
Program water;
Var a:array [1..4,1..3000] of integer;
r:array [1..3] of integer;
i,j,re:integer;
f1,f2:text;
Function cc(w:integer):boolean;
var f:boolean;
k:integer;
begin
f:=true;
for k:=1 to 3 do
if a[k,w]>r[k] then
f:=false;
cc:=f;
end;
Function pd(w:integer):boolean;
var k,s:integer;
f:boolean;
begin
f:=true;
for k:=1 to w-1 do
if a[1,k]=a[1,w] then
if a[2,k]=a[2,w] then
if a[3,k]=a[3,w] then begin
f:=false;
k:=w;
end;
pd:=f;
end;
Function search(w:integer):boolean;
var f:boolean;
k:integer;
begin
f:=false;
for k:=1 to 3 do
if a[k,w]=re then f:=true;
search:=f;
end;
Procedure print(var w:integer);
var b:array [1..1000] of integer;
k,s:integer;
begin
k:=1;
b[k]:=w;
repeat
w:=a[4,w];
k:=k+1;
b[k]:=w;
until w=1;
while k<>0 do begin
s:=b[k];
k:=k-1;
writeln(f2,a[1,s],a[2,s],a[3,s]);
end;
close(f1);
close(f2);
halt;
end;
Procedure splite;
var i,j,k,s:integer;
begin
i:=1;
j:=1;
repeat
for k:=1 to 3 do
if a[k,i]<>0 then
for s:=1 to 3 do
if s<>k then begin
inc(j);
a[s,j]:=a[s,i]+a[k,i];
a[6-k-s,j]:=a[6-k-s,i];
if cc(j)=false then begin
a[k,j]:=a[s,j]-r[s];
a[s,j]:=r[s];
end;
if pd(j)=false then begin
j:=j-1;
if search(j) then
print(j);
end;
end;
inc(i);
until j>=3000;
end;
Begin
assign(f1,'water.in');
reset(f1);
assign(f2,'water.out');
rewrite(f2);
read(f1,r[1],r[2],r[3],re);
for i:=1 to 3000 do
for j:=1 to 4 do
a[j,i]:=0;
a[3,1]:=r[3];
splite;
End.