高精度算法个人分析

1.用数组进行进位与运算。
2.常用于位数过大的数的加减乘除运算。
3.必备知识。
4.模块(仅供参考,若想学到知识,请看懂而后行,杜绝照搬):
高精度乘法:

var
  a,b,c:array [1..500] of integer;
  n1,n2:ansistring;
  lena,lenb,lenc,i,j,x:integer; 
begin
  readln(n1);
  readln(n2);
  lena:=length(n1);
  lenb:=length(n2);
  for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-48;
  for i:=1 to lenb do b[lenb-i+1]:=ord(n2[i])-48; 
   for i:=1 to lena do 
   begin
   x:=0;
      for j:=1 to lenb 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;
  lenc:=lena+lenb;
  while (c[lenc]=0) and (lenc>1) do dec(lenc);
  for i:=lenc downto 1 do write(c[i]);
  writeln;
end.

高精度加法:

var 
  a,b,c:array [1..1002] of integer;
   n:ansistring;
  lena,lenb,lenc,i,x:integer;
 begin 
  readln(n);  
  lena:=length(n);
  for i:=1 to lena do a[lena-i+1]:=ord(n[i])-ord('0');
  readln(n);
  lenb:=length(n);
  for i:=1 to lenb do b[lenb-i+1]:=ord(n[i])-ord('0'); 
  i:=1; x:=0; 
   while (i<=lena) or (i<=lenb) do 
    begin 
    c[i]:=a[i]+b[i]+x;
    x:=c[i] div 10; 
    c[i]:=c[i] mod 10; 
    i:=i+1;
    end;
    if x>0 then 
     begin lenc:=i; c[i]:=x; end
           else lenc:=i-1;
    for i:=lenc downto 1 do write(c[i]);
    writeln;
  end.

高精度减法:

var 
  a,b,c:array [1..500] of integer; 
  n,n1,n2:string; 
  lena,lenb,lenc,i:integer; 
 begin
 readln(n1);
 readln(n2);
  if (length(n1)<length(n2)) or (length(n1)=length(n2)) and (n1<n2) then 
  begin
  n:=n1; n1:=n2; n2:=n;
  write('-');  
  end; 
 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');
 i:=1; 
  while i<=lena do 
  begin 
   if a[i]<b[i] then 
   begin
     a[i]:=a[i]+10; 
     a[i+1]:=a[i+1]-1; 
   end; 
  c[i]:=a[i]-b[i];
  i:=i+1;
  end;
  lenc:=i;
 while (c[lenc]=0) and (lenc>1) do dec(lenc);
 for i:=lenc downto 1 do write(c[i]);
 end.

高精度除法:

const
  n=100;
   type arr=array [1..1000] of integer;
var
  a,b,c:arr;
procedure init;
  var
    st1,st2:string;
    len1,len2,i:longint;
  begin
    readln(st1);
    readln(st2);
    len1:=length(st1);
    len2:=length(st2);
    for i:=1 to len1 do
      a[n-len1+i]:=ord(st1[i])-48;
    for i:=1 to len2 do
      b[n-len2+i]:=ord(st2[i])-48;
  end;
function comp(a,b:arr):boolean;
  var
    i:longint;
  begin
    i:=1;
    while (i<n)and(a[i]=b[i]) do
      inc(i);
    if a[i]>=b[i] then comp:=true
              else comp:=false;
  end;
procedure sub;
  var
    i,g:longint;
  begin
    g:=0;
    for i:=n downto 1 do
      if a[i]>=b[i]+g then begin
         a[i]:=a[i]-b[i]-g;
         g:=0;
      end
      else begin
        a[i]:=10+a[i]-b[i]-g;
        g:=1;
      end;
  end;

procedure ine(n:longint);
  var
    g,i,s:longint;
  begin
    g:=0;
    c[n]:=c[n]+1;
    for i:=n downto 1 do
      begin
        s:=c[i]+g;
        g:=s div 10;
        c[i]:=s mod 10;
      end;
    end;
procedure work;
  var
    i,j:longint;
  begin
    j:=1;
    while comp(a,b) do
      begin
        inc(j);
        for i:=2 to n do
          begin
            b[i-1]:=b[i];
            b[i]:=0;
          end;
        end;
    while j>0 do
      begin
        while comp(a,b)do
          begin
            ine(n-j+1);
            sub;
          end;
        j:=j-1;
        for i:=n downto 2 do
          begin
            b[i]:=b[i-1];
            b[i-1]:=0;
          end;
        end;
      end;
procedure print;
  var
    i,j:longint;
  begin
    j:=1;
    while (j<n)and(c[j]=0) do  inc(j);
    for i:=j to n do
      write(c[i]);
    j:=1;
    write('...');
    while (j<n)and(a[j]=0)do  inc(j);
    for i:=j to n do
      write(a[i]);
    end;
begin
  init;
  work;
  print;
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值