Cod sursa(job #45821)

Utilizator reginaRegina M. regina Data 1 aprilie 2007 23:03:22
Problema Next Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.07 kb
type sir=array[1..100000] of char;
var f,g:text;
    a,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[i];
        a[i]:=a[n-i+1];
        a[n-i+1]:=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:integer;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(48+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,t:integer;i: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])-96+t;
  if x>9 then begin
           t:=1;
           c[i]:=chr(x mod 10 + 48)
  end
    else begin c[i]:=chr(x+48);t:=0; end;
end;

if t<>0 then begin
  p:=p+1;
  c[p]:='1';
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 (a[1]<>'0') then begin
                scad(d,a,p,n,a,n);
                adun(c,m,a,n,c,m);
      end;

      tip(c,m);
end;

close(g);

END.