为防止广告,目前nocow只有登录用户能够创建新页面。如要创建页面请先登录/注册(新用户需要等待1个小时才能正常使用该功能)。
高精度计算
主要的方法是利用数组模拟计算,和笔算差不多,比如:
目录 |
[编辑] 高精度加法
12345678910111213 + 1111111111111111111
开两个数组存储:
a[]={3,1,2,1,1,1,0,1,9,8,7,6,5,4,3,2,1};
b[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
两个数组分别把数值倒存,在一位一位的加,每位加后判断是否大于10,在进位(注:如果太大的数值,可以考虑4位一存哦.) 注意下面的a1,b1,c1 为 数组的长度
if a1>b1 then c1:=a1 else c1:=b1; for i:=1 to c1 do begin c[i]:=a[i]+b[i]+c[i]; c[i+1]:=c[i+1]+c[i]div 10; c[i]:=c[i] mod 10; end; while c[c1+1]>0 do begin inc(c1); c[c1+1]:=c[c1+1]+c[c1]div 10; c[c1]:=c[c1] mod 10; end;
四位一存(这是高精乘,有个小Bug,就当做练习....):
var s:ansistring; a,b:array[1..11000]of int64; x:array[1..51000]of int64; c:array[1..35000]of int64; i,j,a1,b1,c1,d,x1:longint; begin while not eof do begin fillchar(c,sizeof(c),0); readln(s); x1:=length(s); for i:=1 to x1 do x[i]:=ord(s[x1-i+1])-48;{把数字倒过来存} for i:=1 to (x1 div 5) do a[i]:=x[5*i]*10000+x[5*i-1]*1000+x[5*i-2]*100+x[5*i-3]*10+x[5*i-4];{压位} a1:=x1 div 5; if x1 mod 5<>0 then begin d:=0; for i:=1 to x1 mod 5 do d:=d*10+x[x1-i+1]; a1:=a1+1; a[a1]:=d; end;{从最高位一直乘10,处理压位后剩下的数} readln(s); fillchar(x,sizeof(x),0); x1:=length(s); for i:=1 to x1 do x[i]:=ord(s[x1-i+1])-48; for i:=1 to (x1 div 5) do b[i]:=x[5*i]*10000+x[5*i-1]*1000+x[5*i-2]*100+x[5*i-3]*10+x[5*i-4]; b1:=x1 div 5; if x1 mod 5<>0 then begin d:=0; for i:=1 to x1 mod 5 do d:=d*10+x[x1-i+1]; b1:=b1+1; b[b1]:=d; end;{以上是读入的过程} for i:=1 to a1 do for j:=1 to b1 do c[i+j-1]:=c[i+j-1]+a[i]*b[j]; c1:=a1+b1-1; for i:=1 to c1 do if c[i]>99999 then begin c[i+1]:=c[i+1]+c[i] div 100000; c[i]:=c[i] mod 100000; end; while c[c1+1]>0 do begin inc(c1); c[c1+1]:=c[c1+1] + c[c1] div 100000; c[c1]:=c[c1] mod 100000; end; write(c[c1]); for i:=c1-1 downto 1 do begin if c[i]=0 then write('00000') else if c[i]<10 then write('0000',c[i]) else if c[i]<100 then write('000',c[i]) else if c[i]<1000 then write('00',c[i]) else if c[i]<10000 then write('0',c[i]) else write(c[i]); end; writeln; end; end.
[编辑] 高精度减法
var x,y,r:string; la,lb,i,t:integer; a,b:array[1..1000] of integer; begin readln(x); readln(y); la:=length(x); lb:=length(y); if (lb>la) or (lb=la) and (y>x) then begin r:=x; x:=y; y:=r; write('-'); t:=la; la:=lb; lb:=t; end; for i:=1 to la do a[i]:=ord(x[la+1-i])-48; for i:=1 to lb do b[i]:=ord(y[lb+1-i])-48; for i:=1 to la do begin a[i]:=a[i]-b[i]; a[i+1]:=a[i+1]+ord(a[i]>=0)-1; a[i]:=a[i]+ord(a[i]<0)*10; end; t:=la; while (a[t]=0)and(t<>1) do t:=t-1; for i:=t downto 1 do write(a[i]); writeln; end.
--SepHiRoTH 18:59 2009年5月17日 (CST)
[编辑] 高精度乘法
四位一计算:
var he,t,f,w:longint; l1,l2,i,j,temp,temp2,moder,sa,sb,n:integer; s1,s2,str:string; last,a,b,answer,answer2:array[1..1000] of longint; begin readln(s1); readln(s2); sa:=ord(s1[1])-48; sb:=ord(s2[1])-48; l1:=length(s1); l2:=length(s2); if (l2>l1) or ((l2=l1) and (sa<sb)) then begin temp:=l1;l1:=l2;l2:=temp; str:=s1;s1:=s2;s2:=str; end; for i:=1 to l1 do answer[i]:=0; for i:=1 to l2 do answer2[i]:=0; for i:=l1 downto 1 do a[i]:=ord(s1[l1+1-i])-48; for i:=l2 downto 1 do b[i]:=ord(s2[l2+1-i])-48; temp:=1; for i:=1 to l1 do begin moder:=i mod 4; if moder=0 then moder:=4; he:=1; for j:=1 to moder-1 do he:=he*10; answer[temp]:=answer[temp]+a[i]*he; if i mod 4=0 then begin temp:=temp+1; he:=1; end; end; temp2:=1; for i:=1 to l2 do begin moder:=i mod 4; if moder=0 then moder:=4; he:=1; for j:=1 to moder-1 do he:=he*10; answer2[temp2]:=answer2[temp2]+b[i]*he; if i mod 4=0 then begin temp2:=temp2+1; he:=1; end; end; while answer[l1]=0 do l1:=l1-1; while answer[l2]=0 do l2:=l2-1; for i:=1 to l1 do for j:=1 to l2 do begin t:=answer[i]*answer2[j]; w:=i+j-1; last[w]:=last[w]+t; last[w+1]:=last[w+1]+last[w] div 10000; last[w]:=last[w] mod 10000; end; n:=l1+l2; while (last[n]=0) and (n>1) do n:=n-1; write(last[n]); for i:=n-1 downto 1 do begin if last[i]<1000 then write(0); if last[i]<100 then write(0); if last[i]<10 then write(0); write(last[i]); end; end.
更快的算法需要借助FFT(也有人喜欢用NTT) 实现nlogn
[编辑] 高精度除法
只提供程序段,未处理循环小数。
procedure highdevide(a,b:hp; var c,d:hp); //高精度除法 高精度/高精度 var i,len:integer; begin fillchar(c,sizeof(c),0); fillchar(d,sizeof(d),0); len:=a[0];d[0]:=1; for i:=len downto 1 do begin multiply(d,10,d); d[1]:=a[i]; while(compare(d,b)>=0) do {即d>=b} begin Subtract(d,b,d); inc(c[i]); end; end; while(len>1)and(c.s[len]=0) do dec(len); c.len:=len; end; 解释: hp为数组定义 type hp:array[1..max] of integer; a[0],b[0],c[0],d[0]为数组的长度 再在主程序里打出来 如:for i:=x[0] downto 1 do write(x[i]); write('.'); for i:=1 to y[0] do write(y[i]); writeln; x为商的整数部分,y为商的小数部分。
--Taophee 22:24 2011年8月20日
请看到这个问题的OIers注意并及时给出正确解法,最近忙于琐事,拜托了,这个网站很久无人管理了(——什么?!!怎么能这样!)。--SepHiRoTH 23:02 2011年7月30日 (CST)
算法已改。 --Taophee 22:24 2011年8月20日
做一下循环小数,这需要加一段,既然做了就把 它做好怎样?--SepHiRoTH 08:20 2011年8月21日 (CST)
[编辑] 高精度阶乘
作为一种高精度乘法的扩展算法,实质为高精度乘低精度,算法如下:
var a:array[1..10000] of longint; i,j,k,l,p,o,q,x,y,w:integer; begin read(i); a[1]:=1; w:=1; for j:=1 to i do begin y:=0; //到“For”前可省,但改为for k:=1 to 10000 do x:=j; while x>0 do begin y:=y+1; x:=x div 10; end; o:=0; for k:=w to l+y+1 do begin q:=a[k]*j+o; o:=q div 10; a[k]:=q mod 10; end; l:=10000; while (a[l]=0) and (l>1) do l:=l-1; w:=1; while (a[w]=0) and (w<9999) do w:=w+1; end; for p:=l downto 1 do write(a[p]); writeln; end.
--SepHiRoTH 18:59 2009年5月17日 (CST)
[编辑] 高精度快速幂
主要用了二分的手段。中间的乘法就看上面的吧。 --By Clarkok
function Power(a:high;b:longint):high; begin power:=power(a,b shr 1); power:=mult(power,power); if odd(b) then power:=mult(power,a); end;
顺便附送个高精度的单元,FPC可用:高精度单元 完整程序详见高精度程序。