谢谢楼主,请回答我一个问题
题目是这样的:将自然数1到9分成三组,每组三个数,使得每组中的三个数之和皆为指数,请列出所有的分发。 先要用Pascal解决这道题,可我在编程之后,发现输出的数据有不同位重复的现象例如: 8 7 4,6 5 2 ,1 3 9和 8 7 4,6 5 2 ,3 1 9 这应该怎么解决?难道数据还要保存?
程序如下: 求解 谢谢
function
iszhishu(a1:integer):boolean;
var x:integer;
begin
if (a1=2) then
begin
iszhishu := true;
end;
x := 3;
while (x <= trunc(sqrt(a1))) and ((a1 mod x) <> 0) do
x := x+1;
if x > trunc(sqrt(a1)) then
begin
iszhishu := true;
end
else
begin
iszhishu := false;
end;
end;
var
count,i1,i2,i3,i4,i5,i6,i7,i8,i9:integer;
begin
count:=0;
for i1 := 1 to 9 do
begin
if (i1 = i9) or (i1 = i8) or (i1=i7) or (i1=i6)
or (i1=i5) or (i1=i4) or (i1=i3) or (i1=i2) then
continue;
for i2 := 1 to 9 do
begin
if (i2 = i9) or (i2 = i8) or (i2=i7) or (i2=i6)
or (i2=i5) or (i2=i4) or (i2=i3) or (i2=i1) then
continue;
for i3 :=1 to 9 do
begin
if (i3 = i9) or (i3 = i8) or (i3=i7) or (i3=i6)
or (i3=i5) or (i3=i4) or (i3=i2) or (i3=i1) then
continue;
for i4 :=1 to 9 do
begin
if (i4 = i9) or (i4 = i8) or (i4=i7) or (i4=i6)
or (i4=i5) or (i4=i3) or (i4=i2) or (i4=i1) then
continue;
for i5 :=1 to 9 do
begin
if (i5 = i9) or (i5 = i8) or (i5=i7) or (i5=i6)
or (i5=i4) or (i5=i3) or (i5=i2) or (i5=i1) then
continue;
for i6 :=1 to 9 do
begin
if (i6 = i9) or (i6 = i8) or (i6=i7) or (i6=i5)
or (i6=i4) or (i6=i3) or (i6=i2) or (i6=i1) then
continue;
for i7 :=1 to 9 do
begin
if (i7 = i9) or (i7 = i8) or (i7=i6) or (i7=i5)
or (i7=i4) or (i7=i3) or (i7=i2) or (i7=i1) then
continue;
for i8 :=1 to 9 do
begin
if (i8 = i9) or (i8 = i7) or (i8=i6) or (i8=i5)
or (i8=i4) or (i8=i3) or (i8=i2) or (i8=i1) then
continue;
for i9 :=1 to 9 do
begin
if (i9 = i8) or (i9 = i7) or (i9=i6) or (i9=i5)
or (i9=i4) or (i9=i3) or (i9=i2) or (i9=i1) then
continue;
if iszhishu(i1+i2+i3) and iszhishu(i4+i5+i6)
and iszhishu(i7+i8+i9) then
begin
count := count + 1;
writeln(count,',',i1,',',i2,',',i3,',',i4,',',i5,
',',i6,',',i7,',',i8,',',i9);
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end.
题目是这样的:将自然数1到9分成三组,每组三个数,使得每组中的三个数之和皆为指数,请列出所有的分发。 先要用Pascal解决这道题,可我在编程之后,发现输出的数据有不同位重复的现象例如: 8 7 4,6 5 2 ,1 3 9和 8 7 4,6 5 2 ,3 1 9 这应该怎么解决?难道数据还要保存?
程序如下: 求解 谢谢
function
iszhishu(a1:integer):boolean;
var x:integer;
begin
if (a1=2) then
begin
iszhishu := true;
end;
x := 3;
while (x <= trunc(sqrt(a1))) and ((a1 mod x) <> 0) do
x := x+1;
if x > trunc(sqrt(a1)) then
begin
iszhishu := true;
end
else
begin
iszhishu := false;
end;
end;
var
count,i1,i2,i3,i4,i5,i6,i7,i8,i9:integer;
begin
count:=0;
for i1 := 1 to 9 do
begin
if (i1 = i9) or (i1 = i8) or (i1=i7) or (i1=i6)
or (i1=i5) or (i1=i4) or (i1=i3) or (i1=i2) then
continue;
for i2 := 1 to 9 do
begin
if (i2 = i9) or (i2 = i8) or (i2=i7) or (i2=i6)
or (i2=i5) or (i2=i4) or (i2=i3) or (i2=i1) then
continue;
for i3 :=1 to 9 do
begin
if (i3 = i9) or (i3 = i8) or (i3=i7) or (i3=i6)
or (i3=i5) or (i3=i4) or (i3=i2) or (i3=i1) then
continue;
for i4 :=1 to 9 do
begin
if (i4 = i9) or (i4 = i8) or (i4=i7) or (i4=i6)
or (i4=i5) or (i4=i3) or (i4=i2) or (i4=i1) then
continue;
for i5 :=1 to 9 do
begin
if (i5 = i9) or (i5 = i8) or (i5=i7) or (i5=i6)
or (i5=i4) or (i5=i3) or (i5=i2) or (i5=i1) then
continue;
for i6 :=1 to 9 do
begin
if (i6 = i9) or (i6 = i8) or (i6=i7) or (i6=i5)
or (i6=i4) or (i6=i3) or (i6=i2) or (i6=i1) then
continue;
for i7 :=1 to 9 do
begin
if (i7 = i9) or (i7 = i8) or (i7=i6) or (i7=i5)
or (i7=i4) or (i7=i3) or (i7=i2) or (i7=i1) then
continue;
for i8 :=1 to 9 do
begin
if (i8 = i9) or (i8 = i7) or (i8=i6) or (i8=i5)
or (i8=i4) or (i8=i3) or (i8=i2) or (i8=i1) then
continue;
for i9 :=1 to 9 do
begin
if (i9 = i8) or (i9 = i7) or (i9=i6) or (i9=i5)
or (i9=i4) or (i9=i3) or (i9=i2) or (i9=i1) then
continue;
if iszhishu(i1+i2+i3) and iszhishu(i4+i5+i6)
and iszhishu(i7+i8+i9) then
begin
count := count + 1;
writeln(count,',',i1,',',i2,',',i3,',',i4,',',i5,
',',i6,',',i7,',',i8,',',i9);
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end.
