я не знаю как решить ребус VOLVO+FIAT=MOTOR. Пожалуйста кто-нибудь помогите решить его!!!
Заранее благодарен, eXTreMe
{В результате - 12 решений...
VOLVO+FIAT=MOTOR;
}
uses crt;
var
v, o, l, f, i, a, t, m, r: 0 .. 9;
dig : set of 0..9;
sum, motor: longint;
begin
clrscr;
for v:=0 to 9 do begin
include(dig,v);
for o:=0 to 9 do if not (o in dig) then begin
include(dig,o);
for l:=0 to 9 do if not (l in dig) then begin
include(dig,l);
for f:=0 to 9 do if not (f in dig) then begin
include(dig,f);
for i:=0 to 9 do if not (i in dig) then begin
include(dig,i);
for a:=0 to 9 do if not (a in dig) then begin
include(dig,a);
for t:=0 to 9 do if not (t in dig) then begin
include(dig,t);
for m:= 0 to 9 do if not (m in dig) then begin
include(dig, m);
for r := 0 to 9 do if not (r in dig) then begin
include(dig, r);
sum := v*10000 + (o+f)*1000 + (l+i)*100 + (v+t)*10 + (o+t);
motor := m*10000+o*1000+t*100+o*10+r;
if sum = motor then
writeln(':', v,o,l,v,o,'+',f,i,a,t,'=', m,o,t,o,r);
exclude(dig, r)
end;
exclude(dig, m);
end;
exclude(dig,t);
end;
exclude(dig,a);
end;
exclude(dig,i);
end;
exclude(dig,f);
end;
exclude(dig,l);
end;
exclude(dig,o);
end;
exclude(dig,v);
end;
end.
Var
X: Byte;
S: Set Of Byte;
uses crt;
var
dig: set of byte;
var
first, second, res: string;
T: string;
function check(s, what: string): longint;
var
i: byte;
n: longint;
begin
n := 0;
for i := 1 to length(s) do begin
n := 10 * n;
n := n + ( ord(what[ pos(s[i], T) ]) - 48 );
end;
check := n;
end;
procedure full(s: string);
var i: integer;
begin
if length(s) = length(T) then begin
if check(first, s) + check(second, s) = check(res, s) then
writeln('found: ', check(first, s), '+', check(second, s), '=',
check(res, s));
end
else
for i := 0 to 9 do
if not (i in dig) then begin
include(dig, i);
full(s + chr(48 + i));
exclude(dig, i);
end
end;
procedure fill_str(var s: string; what: string);
var i: byte;
begin
for i := 1 to length(what) do
if pos(what[i], s) = 0 then s := s + what[i];
end;
begin
T := '';
first := 'volvo'; second := 'fiat'; res := 'motor';
fill_str(T, first); fill_str(T, second); fill_str(T, res);
full('');
end.