由1..9这九个数字组成的九位数(无重复数字)能被11整除,求最大,最小值
问题描述:
由1..9这九个数字组成的九位数(无重复数字)能被11整除,求最大,最小值
要free pascal的,别的不要!
答
Min:123475869
Max:987652413
Code:
var a:array[1..9] of longint;
v:array[1..9] of boolean;
i:longint;
min,max:longint;
procedure tryit(x:longint);
var i:longint;
sum:longint;
begin
if x>=10 then begin
sum:=a[1]*100000000+a[2]*10000000+a[3]*1000000+a[4]*100000+a[5]*10000
+a[6]*1000+a[7]*100+a[8]*10+a[9];
if sum mod 11 = 0 then begin
if min = 0 then min:=sum else max:=sum;
end;
exit;
end;
for i:=1 to 9 do if v[i] then begin
v[i]:=false;
a[x]:=i;
tryit(x+1);
v[i]:=true;
end;
end;
begin
for i:=1 to 9 do v[i]:=true;
min:=0;
max:=0;
tryit(1);
if max = 0 then max:=min;
writeln('Min:',min);
writeln('Max:',max);
end.