Cod sursa(job #45638)

Utilizator reginaRegina M. regina Data 1 aprilie 2007 19:20:47
Problema Next Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.01 kb
type sir=array[1..1000001] of char;
var f,g:text;
    a,b,c,d:sir;
    n,m,p:longint;

procedure cit(var a:sir;var n:longint);
begin
n:=0;
while not eoln(f) do begin
  n:=n+1;
  read(f,a[n]);
end;
readln(f);
end;

procedure og(var a:sir;n:longint);
var i:longint;aux:char;
begin
for i:=1 to n div 2 do begin
aux:=a[n-i+1];
a[n-i+1]:=a[i];
a[i]:=aux;
end;
end;

procedure tip(a:sir;n:longint); forward;

procedure scad(a,b:sir;n,m:longint;var c:sir;var p:longint);
var t,x,i:longint;
begin
t:=0;
og(a,n);
og(b,m);
for i:=m+1 to n do b[i]:='0';

for i:=1 to n do begin
      x:=ord(a[i])-ord(b[i])+t;
      if x<0 then begin x:=x+10;t:=-1 end;
      c[i]:=chr(ord('0')+x)
end;

og(c,n);

while (c[1]='0')and(n>1) do begin
  for i:=1 to n-1 do c[i]:=c[i+1];
  n:=n-1;
  end;

if n<>0 then p:=n
else begin n:=1; c[1]:='0' end

end;

procedure tip(a:sir;n:longint);
var i:longint;
begin
for i:=1 to n do
  write(g,a[i]);
end;

function compar(a:sir;n:longint;d:sir;p:longint):boolean;
var i:longint;
begin
if n>p then compar:=true
  else if n<p then compar:=false
    else begin
      i:=1;
      while (a[i]=d[i])and(i<n) do i:=i+1;
      if a[i]>=d[i] then compar:=true
        else compar:=false;
    end;
end;

procedure adun(a:sir;n:longint;b:sir;m:longint;var c:sir;var p:longint);
var x,i,t:longint;
begin
t:=0;
p:=n;
og(a,n);og(b,m);
for i:=m+1 to n do b[i]:='0';
for i:=1 to n do begin
  x:=ord(a[i])+ord(b[i])-2*ord('0')+t;
  if x>9 then begin
    t:=1;
    c[i]:=chr(x mod 10+ord('0'))
  end
    else begin c[i]:=chr(x+ord('0'));t:=0; end;
end;
if t<>0 then begin
  p:=p+1;
  c[p]:=chr(t+ord('0'));
end;
og(c,p);
end;


BEGIN

assign(f,'next.in'); reset(f);
assign(g,'next.out');rewrite(g);
cit(a,n);  c:=a;m:=n;
cit(d,p);
if not compar(a,n,d,p) then tip(d,p)
else begin
while compar(a,n,d,p) do scad(a,d,n,p,a,n);

if (n=1)and(a[1]='0') then
else begin scad(d,a,p,n,a,n);
           adun(c,p,a,n,c,m);
end;
tip(c,m);
end;
close(g);
END.