世界第一难的pascal题,求所有4位数的黑洞数步骤

来源:百度知道 编辑:UC知道 时间:2024/07/01 04:39:38
谁能帮小弟找找错在那里?编译时可以成功,就是运行不了,会显示:
exited with
exitcode=201
程序如下
program heidong;
var a:array[1..4]of integer;
hd,shiyan,i,j,temp,big,small,w,result:integer;
begin
for hd:=1000 to 9999 do
begin
shiyan:=hd;
for i:=1 to 4 do
begin
a[i]:=shiyan mod 10;
shiyan:=shiyan div 10;
end;
write(hd,' needs: ');
w:=0;
if a[1]<>a[2] then
begin
repeat
for i:=1 to 3 do
for j:=i+1 to 4 do
if a[i]<a[j] then
begin
temp:=a[i];
a[i]:=a[j];
a[j]:=temp;
end;
big:=a[4]+a[3]*10+a[2]*100+a[1]*1000;
small:=a[1]+a[2]*10+a[3]*100+a[4]*1000;
result:=big-small;
shiyan:=result;
for i:=1 to 4 do
begin
a[i]:=shiyan mod 10;
shiyan:=shiyan div 10;
end;
inc(w);
until result=6147;
w

(1)你的黑洞数错了,应该是6174不是6147
(2)你判断wrong的条件不对,而且不应该放在外面。
修改后的程序:
program heidong;
var a:array[1..4]of integer;
hd,shiyan,i,j,temp,big,small,w,result:integer;
begin
for hd:=1000 to 9999 do
begin
shiyan:=hd;
for i:=1 to 4 do
begin
a[i]:=shiyan mod 10;
shiyan:=shiyan div 10;
end;
write(hd,' needs: ');
w:=0;
repeat
for i:=1 to 3 do
for j:=i+1 to 4 do
if a[i]<a[j] then
begin
temp:=a[i];
a[i]:=a[j];
a[j]:=temp;
end;
big:=a[4]+a[3]*10+a[2]*100+a[1]*1000;
small:=a[1]+a[2]*10+a[3]*100+a[4]*1000;
result:=big-small;
shiyan:=result;
for i:=1 to 4 do
begin
a[i]:=shiyan mod 10;
shiyan:=shiyan div 10;
end;
inc(w);
until (result=6174) or (result=0);
if result<>0 then writeln