POJ 1001 (坑爹的小数高精度乘法)

来源:互联网 发布:手机app监控软件 编辑:程序博客网 时间:2024/05/01 14:52

小数高精度乘法


program P1001;Type    arr=record        a:array[1..10000] of longint;        len:longint;        xs:longint;        end;Var    s:ansistring;    n,i,j,k,len:longint;    r:arr;Procedure relax(var c:arr);var   i,j:longint;begin   while (c.a[c.len]=0) and (c.len>1) do dec(c.len);   if (c.len=1) and (c.a[1]=0) then begin c.xs:=0; exit; end;   i:=1;   while (c.a[i]=0) do inc(i);   dec(i);   if (i>0) then   begin      if (c.xs>=i) then dec(c.xs,i)      else      begin         i:=c.xs;         c.xs:=0;      end;      for j:=i+1 to c.len do c.a[j-i]:=c.a[j];      dec(c.len,i);   end;end;procedure prin(c:arr);var   i:longint;begin   relax(c);   if (c.xs=0) then   begin      for i:=c.len downto 1 do write(c.a[i]);      writeln;      exit;   end;   if (c.xs<=c.len) then   begin      for i:=c.len downto c.xs+1 do write(c.a[i]);      write('.');      for i:=c.xs downto 1 do write(c.a[i]);      writeln;      exit;   end;   write('.');   for i:=1 to c.xs-c.len do write('0');   for i:=c.len downto 1 do write(c.a[i]);   writeln;end;Procedure cheng(a:arr;b:arr;var c:arr);var   i,j:longint;begin   fillchar(c,sizeof(c),0);   for i:=1 to a.len do      for j:=1 to b.len do      begin         inc(c.a[i+j-1],a.a[i]*b.a[j]);         inc(c.a[i+j],c.a[i+j-1] div 10);         c.a[i+j-1]:=c.a[i+j-1] mod 10;      end;   c.len:=a.len+b.len+1;   c.xs:=a.xs+b.xs;   relax(c);end;procedure jie(a:arr;b:longint;var c:arr);begin   if (b=1) then c:=a   else if (b mod 2=0) then   begin      jie(a,b div 2,c);      cheng(c,c,c);   end   else begin      jie(a,b div 2,c);      cheng(c,c,c);      cheng(c,a,c);   end;end;Begin   while (not(eof)) do   begin      readln(s);      len:=length(s);      i:=pos(' ',s);      j:=pos('.',s);      if j=0 then      begin         r.xs:=0;         r.len:=i-1;         for k:=1 to r.len do r.a[r.len-k+1]:=ord(s[k])-48;      end      else begin      r.xs:=i-j-1;      r.len:=i-2;      for k:=1 to j-1 do r.a[r.len-k+1]:=ord(s[k])-48;      for k:=j+1 to i-1 do r.a[r.len-k+2]:=ord(s[k])-48;      end;      relax(r);      i:=len;      while (s[i]<>' ') do dec(i);      n:=0;      for k:=i+1 to len do n:=n*10+ord(s[k])-48;      jie(r,n,r);      prin(r);  end;end.


原创粉丝点击