您的位置:首页 > 其它

高精度

2014-10-28 21:53 309 查看
高精度是基本知识,先从简单到难


最基本:
1.高精度加法(高精度数加高精度数)

</pre><pre name="code" class="html"><span style="font-size:18px;">function jia(t1,t2:string):string;
var
a,b,c:arr;//程序定义时写:type arr=array[1..1000]of integer;
x,i,max:longint;//x:进位标记
begin
fillchar(a,sizeof(a),0);b:=a;
for i:=1 to length(t1) do a[length(t1)-i+1]:=ord(t1[i])-ord('0');
for i:=1 to length(t2) do b[length(t2)-i+1]:=ord(t2[i])-ord(‘0’);
if length(t1)>length(t2) then max:=length(t1) else max:=length(t2);
x:=0;
for i:=1 to max do
begin
c[i]:=a[i]+b[i]+x;
x:=c[i] div 10;
c[i]:=c[i] mod 10;
end;
if x>0 then begin inc(max);c[max]:=x; end;
jia:='';
for i:=max downto 1 do jia:=jia+chr(c[i]+ord('0'));
end;</span>
2.高精度减法(高精度数减高精读数)

<span style="font-size:18px;">function jian(t1,t2:string):string
var
a,b,c:arr;//程序定义时写:type arr=array[1..1000]of integer;
i,max:longint;
t:string;//交换
begin
if (length(t1)<length(t2))or((length(t1)=length(t2))and(t1<t2)) then
begin
jian:='-';t:=t1;t1:=t2;t2:=t;
end
else jian:='';
fillchar(a,sizeof(a),0);b:=a;
for i:=1 to length(t1) do a[length(t1)-i+1]:=ord(t1[i])-ord('0');
for i:=1 to length(t2) do b[length(t2)-i+1]:=ord(t2[i])-ord('0');
if length(t1)>length(t2) then max:=length(t1) else max:=length(t2);
for i:=1 to max do
begin
c[i]:=a[i]-b[i];
if c[i]<0 then begin c[i]:=c[i]+10;dec(a[i+1]); end;
end;
while (max>1)and(c[max]=0) do dec(max);
for i:=max downto 1 do jian:=jian+chr(c[i]+ord('0'));
end;</span>
稍微有点麻烦:

3.高精度乘法
<span style="font-size:18px;"><span style="color:#362e2b;">function cheng(t1,t2:string):string;
var
a,b,c:arr;//程序定义时写:type arr=array[1..1000]of integer;
x,i,j,w:longint;//x:进位标记  w:位数
begin
fillchar(a,sizeof(a),0);b:=a; c:=a;
for i:=1 to length(t1) do a[length(t1)-i+1]:=ord(t1[i])-ord('0');
for i:=1 to length(t2) do b[length(t2)-i+1]:=ord(t2[i])-ord('0');
for i:=1 to length(t1) do
begin
x:=0;
for j:=1 to length(t2) do
begin
c[i+j-1]:=a[i]*b[j]+x+c[i+j-1];
x:=c[i+j-1] div 10;
c[i+j-1]:=c[i+j-1] mod 10;
end;
c[i+j]:=x;
end;
w:=length(t1)+length(t2);
while (c[w]=0)and(w>1) do dec(w);
cheng:='';
for i:=w downto 1 do cheng:=cheng+chr(c[i]+ord('0'));
end;</span></span>
可以为普及组第三题或第四题,提高组第二题或第三题,即使会方法,打程序的时候都易出错的:

4.高精度数除以高精度数的整数部分(须挪用前面三个函数)

<span style="font-size:18px;"><span style="color:#362e2b;">function chu(p1,t2:ansistring):ansistring;
var
s1:array[0..9]of ansistring;
ws:array[1..9]of longint;
i,wi,wt2,wt1,x,t,fw:longint;
shi,t1,f:ansistring;
function cha(f1,f2:ansistring):longint;
var
i,wf1,w:longint;
begin
wf1:=length(f1);
for i:=1 to 9 do
begin
w:=length(s1[i]);
if (w>wf1)or((w=wf1)and(s1[i]>f1)) then
begin
cha:=i-1;
break;
end;
end;
end;
begin
t1:=p1;wt1:=length(t1);wt2:=length(t2); chu:=''; s1[0]:='0';
for i:=1 to 9 do
begin
s1[i]:=cheng(t2,chr(i+ord('0')));
ws[i]:=length(s1[i]);
end;
shi:='';i:=1;
while (i<=wt1) do
begin
shi:=shi+t1[i];
t:=cha(shi,t2);
chu:=chu+chr(t+ord('0'));
if t<>0 then
begin
delete(t1,1,i);
f:=jian(shi,s1[t]);
if f<>'0' then
begin
t1:=f+t1;
shi:=f;i:=length(f);
end
else begin shi:='';i:=0; end;
wt1:=length(t1);
end;
inc(i);
end;
while chu[1]='0' do delete(chu,1,1);
end;</span></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pascal 高精度