www.pudn.com > TJU.rar > ac1062.pas


program tju1062; 
var 
  s:string; 
  a,b,c,p,q:array[1..6]of byte; 
  ans,i:byte; 
procedure search(l,d,m:word); 
  var 
    x,y,z,i:word; 
  begin 
    if m>=ans then exit; 
    if l=0 then begin ans:=m;exit;end; 
    for x:=0 to 9 do begin 
      p[l]:=x; 
      for y:=0 to 9 do begin 
        q[l]:=y; 
        z:=d; 
        for i:=l to 6 do 
          inc(z,p[i]*q[6+l-i]); 
        search(l-1,z div 10,m+ord(x<>a[l])+ord(y<>b[l])+ord(z mod 10<>c[l])); 
        if m>=ans then exit; 
      end; 
    end; 
  end; 
begin 
  repeat 
    readln(s); 
    for i:=1 to 6 do begin 
      a[i]:=ord(s[i])-48; 
      b[i]:=ord(s[7+i])-48; 
      c[i]:=ord(s[14+i])-48; 
    end; 
    ans:=6; 
    search(6,0,0); 
    writeln(ans); 
  until seekeof; 
end.