高精度减法

高精度减法也比较简单,与加法很相似,只贴一下代码吧:

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
                if a[i]>=b[i] then
                        c[i]:=a[i]-b[i]
                else
                begin
                        dec(a[i+1]);
                        c[i]:=a[i]+10-b[i];
                end;
        inc(len1);
        while c[len1]=0 do dec(len1);
        for i:=len1 downto 1 do
                write(c[i]);
        writeln;
end.


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