天天看點

洛谷 1601 A+B Problem(高精)(The Last Problem In MY Winter Holiday,the Thirtieth)題目題解代碼

題目

高精度加法,給出a,b,求和
           

題解

高精度加法,注意a和b都是0的情況
 時間複雜度O(500)
           

代碼

var
  a,b,c:array[..]of longint;
  i,j,n:longint;
  s,t:ansistring;

procedure add;
var
  x,i,j,k:longint;
begin
  i:=;x:=;
  while i<=n do
    begin
      c[i]:=a[i]+b[i]+x;
      x:=c[i] div ;
      c[i]:=c[i] mod ;
      inc(i);
    end;
  if x> then c[i]:=x;
end;

begin
  readln(s);
  j:=length(s);
  for i:= to j do
    a[j-i+]:=ord(s[i])-ord('0');
  readln(t);
  n:=j;
  j:=length(t);
  if n<j then n:=j;
  for i:= to j do
    b[j-i+]:=ord(t[i])-ord('0');
  if (s='0')and(t='0') then begin writeln('0');halt;end;
  add;
  i:=;
  while c[i]= do dec(i);
  for j:=i downto  do
    write(c[j]);
end.