高精度加法

做高精度加法其实只要在草稿纸上画一下,模拟一下这个过程就很容易做出来了:

代码:

var
        len1,len2,i:longint;
        a,b,c:array[0..10010] of Longint;
        s1,s2,s:ansistring;
begin
        readln(s1);
        readln(s2);
        if (length(s1)<length(s2)) or ((length(s1)=length(s2)) and (s1<s2)) then
        begin
                s:=s1; s1:=s2; s2:=s; 
        end;
        len1:=length(s1);
        len2:=length(s2);
        for i:=1 to len1 do
                a[len1-i+1]:=ord(s1[i])-48;
        for i:=1 to len2 do
                b[len2-i+1]:=ord(s2[i])-48;
        for i:=1 to len1 do
        begin
                c[i]:=c[i]+a[i]+b[i];
                c[i+1]:=c[i] div 10;
                c[i]:=c[i] mod 10;
        end;
        inc(len1);
        while c[len1]=0 do dec(len1);
        for i:=len1 downto 1 do
                write(c[i]);
        writeln;
end.

你可能感兴趣的:(高精度加法)