- 高精度乘法
- 2014-12-22 19:31:06 @
var a,b,c:array[-10001..10001] of char;
d:array[-10001..10001] of longint;
i,j,t,n,k,x:longint;
begin
i:=1;j:=1;fillchar(d,sizeof(d),0);
fillchar(a,sizeof(a),' ');
fillchar(b,sizeof(b),' ');
repeat
read(a[i]);inc(i);
until eoln;
readln;
repeat
read(b[j]);inc(j);
until eoln;
if i<j then
begin
t:=i;i:=j;j:=t;c:=a;a:=b;b:=c;
end;
dec(i);dec(j);n:=j;k:=0;
repeat
t:=i;
repeat
d[t+k]:=(ord(a[t])-48)*(ord(b[j])-48)+d[t+k];
if d[t+k+1]>9 then
begin
d[t+k]:=d[t+k+1] div 10+d[t+k];
d[t+k+1]:=d[t+k+1] mod 10;
end;
dec(t);
until t=0;
dec(j);dec(k);
until j=0;
repeat
inc(k);
if d[k]>0 then
begin
for x:=k to i do
write(d[x]);
k:=x;
end;
until k=i;
end.
最后一个数据超了16ms,也是醉了。。。
求大神来看看,能不能优化一下。。。
1 条评论
-
Accepted++ LV 8 @ 2016-08-07 13:28:28
type high=array[0..20000]of longint;
var a,b:high;
procedure init(var h:high);
var s:ansistring; i:integer;
begin
readln(s);
h[0]:=length(s);
for i:=1 to h[0] do h[i]:=ord(s[h[0]-i+1])-48;
end;
procedure swap(var a,b:high);
var t:high;
begin
t:=a; a:=b; b:=t;
end;
procedure highmultiply;
var i,j:integer; h:high;
begin
fillchar(h,sizeof(h),0);
h[0]:=a[0];
for i:=1 to a[0] do
for j:=1 to b[0] do h[i+j-1]:=h[i+j-1]+a[i]*b[j];
while(h[h[0]]>0)do inc(h[0]);
dec(h[0]);
for i:=2 to h[0] do
begin
h[i]:=h[i-1]div 10+h[i];
h[i-1]:=h[i-1]mod 10;
end;
while(h[h[0]]>0)do
begin
inc(h[0]);
h[h[0]]:=h[h[0]-1]div 10;
h[h[0]-1]:=h[h[0]-1]mod 10;
end;
for i:=h[0]-1 downto 1 do write(h[i]);
end;
begin
init(a);
init(b);
if(a[0]<b[0])then swap(a,b);
highmultiply;
end.
- 1