Cod sursa(job #45778)

Utilizator reginaRegina M. regina Data 1 aprilie 2007 21:34:26
Problema Next Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2 kb
type sir=array[1..100000]of byte;
var f,g:text;
    a,c,d:sir;
    n,m,p:longint;u:char;

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

procedure og(var a:sir;n:longint);
var i:longint;x,aux:byte;
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 x:byte;i:longint; t:shortint;
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:=a[i]-b[i]+t;
      if x<0 then begin x:=x+10;t:=-1 end;
      c[i]:=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 i:longint;x:byte; t:shortint;

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:=a[i]+b[i]+t;
  if x>9 then begin
    t:=1;
    c[i]:=x mod 10;
              end
  else begin c[i]:=x; t:=0; end;
end;

if t<>0 then begin
  p:=p+1;
  c[p]:=t;
  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,m,a,n,c,m);
end;
tip(c,m);
end;
close(f);
close(g);
END.