高精度计算
高精度数的定义:
type
hp=array[1..maxlen] of integer;
1.高精度加法
procedure plus ( a,b:hp; var c:hp);
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
if a[0]>b[0] then len:=a[0] else len:=b[0];
for i:=1 to len do begin
inc(c[i],a[i]+b[i]);
if c[i]>10 then begin dec(c[i],10); inc(c[i+1]); end; {进位}
end;
if c[len+1]>0 then inc(len);
c[0]:=len;
end;{plus}
2.高精度减法
procedure substract(a,b:hp;var c:hp);
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
if a[0]>b[0] then len:=a[0] else len:=b[0];
for i:=1 to len do begin
inc(c[i],a[i]-b[i]);
if c[i]<0 then begin inc(c[i],10);dec(c[i+1]); end;
while (len>1) and (c[len]=0) do dec(len);
c[0]:=len;
end;
3.高精度乘以低精度
(1)
procedure multiply(a:hp;b:longint;var c:hp);
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
len:=a[0];
for i:=1 to len do begin
inc(c[i],a[i]*b);
inc(c[i+1],(a[i]*b) div 10);
c[i]:=c[i] mod 10;
end;
inc(len);
while (c[len]>=10) do begin {处理最高位的进位}
c[len+1]:=c[len] div 10;
c[len]:=c[len] mod 10;
inc(len);
end;
while (len>1) and (c[len]=0) do dec(len); {若不需进位则调整len}
c[0]:=len;
end;{multiply}
(2)
program jk;
const maxn=1000;
type hp=array[0..maxn] of longint;
var i,j,n:longint; a:hp; b:longint;
procedure mul(var h:hp;k:longint);
//h:=h*k
var i:longint;
begin
for i:=0 to maxn do h[i]:=h[i]*k;
for i:=0 to maxn-1 do
begin
h[i+1]:=h[i+1]+h[i] div 10;
h[i]:=h[i] mod 10
end;
end;
begin
a[1]:=100; //两个乘数
b:=888;
mul(a,b); //求a:=a*b 即888*100
i:=maxn;
while (i>0) and (a[i]=0) do i:=i-1;
for j:=i downto 1 do write(a[j]); //输出计算后a的值
writeln;
end.
4.高精度乘以高精度
(1)
procedure high_multiply(a,b:hp; var c:hp);
var i,j,len:integer;
begin
fillchar(c,sizeof(c),0);
for i:=1 to a[0] do
for j:=1 to b[0] do begin
inc(c[i+j-1],a[i]*b[j]);
inc(c[i+j],c[i+j-1] div 10);
c[i+j-1]:=c[i+j-1] mod 10;
end;
len:=a[0]+b[0]+1;
while (len>1) and (c[len]=0) do dec(len);
c[0]:=len;
end;
(2)
var n1,n2,n3:string;
function mul(n1,n2:string):string;
var a,b,c:array[1..200] of 0..9;
lena,lenb,lenc,i,j,x:integer;
s:string; ch:string;
begin
lena:=length(n1); lenb:=length(n2);
for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord('0');
for i:=1 to lenb do b[lenb-i+1]:=ord(n2[i])-ord('0');
for i:=1 to lena do
begin
x:=0;
for j:=1 to lenb do
begin
x := a[i]*b[j]+x div 10+c[i+j-1];
c[i+j-1]:=x mod 10;
end;
c[i+j]:= x div 10;
end;
lenc:=i+j;
while (c[lenc]=0) and (lenc>1) do dec(lenc);
for i:=lenc downto 1 do
begin
str(c[i],ch);
s:=s+ch;
end;
mul:=s;
end;
begin
assign(input,'input.dat');reset(input);
assign(output,'output.dat');rewrite(output);
readln(n1); readln(n2);
n3:=mul(n1,n2);
write(n3);
close(input);close(output);
end.
5.高精度除以低精度
(1)procedure devide(a:hp;b:longint; var c:hp; var d:longint);
{c:=a div b; d:= a mod b}
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
len:=a[0]; d:=0;
for i:=len downto 1 do begin
d:=d*10+a[i];
c[i]:=d div b;
d:=d mod b;
end;
while (len>1) and (c[len]=0) then dec(len);
c[0]:=len;
end;
(2)
program jk;
const maxn=1000;
type hp=array[0..maxn] of longint;
var i,j,n:longint; c:hp; b:longint;
procedure devide(var h:hp;k:longint);
//h:=h div k
var d,i,r:longint;
begin
r:=0;
for i:=maxn downto 0 do
begin
d:=10*r+h[i];
h[i]:=d div k;
r:=d mod k
end;
end;
begin
c[1]:=13; //被除数
b:=5; //除数
devide(c,b); //求c:=c div b 即13 div 5
i:=maxn;
while (i>0) and (c[i]=0) do i:=i-1;
for j:=i downto 1 do write(c[j]); //输出计算后c的值
writeln;
end.
6.高精度除以高精度
procedure high_devide(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;
7.精确计算n!
const max=10000; n=2000;
var a:array[1..max]of 0..9;
i,j,k,x:integer;
begin
k:=1; a[k]:=1;{a=1}
for i:=2 to n do{a←1*2*3….*n}
begin
x:=0;{进位初始化}
for j:=1 to k do{a=a*i}
begin
x:=x+a[j]*i; a[j]:=x mod 10;x:=x div 10
end;
while x>0 do {处理最高位的进位}
begin
k:=k+1;a[k]:=x mod 10;x:=x div 10
end
end;
for i:=k downto 1 do write(a[i]); {输出a}
end.