高精度乘法

高精度乘法也很简单,只贴下代码:

var
        len1,len2,i,j:longint;
        a,b,c:array[0..1000] 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
                for j:=1 to len2 do
                begin
                        c[i+j-1]:=c[i+j-1]+a[i]*b[j];
                        c[i+j]:=c[i+j]+c[i+j-1] div 10;
                        c[i+j-1]:=c[i+j-1] mod 10;
                end;
        len1:=len1+len2+1;
        while c[len1]=0 do dec(len1);
        for i:=len1 downto 1 do
                write(c[i]);
        writeln;
end.


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