切换到宽版
  • 4944阅读
  • 1回复

[急求] 高精度乘法 [复制链接]

上一主题 下一主题
离线tjzx
 
只看楼主 倒序阅读 0 发表于: 2006-11-17
[急求] 高精度乘法
请您 附高精度乘法的程序及此每一步的具体说明;
或说明以下例程的思想及分析每一步的具体实现方式(解读例程。)谢谢!
ps:请于11.18日9:30前回复本贴!不胜感激!

.高精度乘高精度

程序如下:

program HighPrecision4_Multiply2;
const
fn_inp='hp4.inp';
fn_out='hp4.out';
maxlen=100; { max length of the number }
type
hp=record
    len:integer; { length of the number }
    s:array[1..maxlen] of integer
    { s[1]   is the lowest position
      s[len] is the highest position }
  end;
var
x:array[1..2] of hp;
y:hp; { x:input ; y:output }

procedure PrintHP(const p:hp);
var i:integer;
begin
  for i:=p.len downto 1 do write(p.s);
end;

procedure init;
var
  st:string;
  j,i:integer;
begin
  assign(input,fn_inp);
  reset(input);
  for j:=1 to 2 do
  begin
    readln(st);
    x[j].len:=length(st);
    for i:=1 to x[j].len do { change string to HP }
    x[j].s:=ord(st[x[j].len+1-i])-ord('0');
  end;
  close(input);
end;

procedure Multiply(a,b:hp;var c:hp); { c:=a+b }
var i,j,len:integer;
begin
  fillchar(c,sizeof(c),0);
  for i:=1 to a.len do
    for j:=1 to b.len do
    begin
    inc(c.s[i+j-1],a.s*b.s[j]);
    inc(c.s[i+j],c.s[i+j-1] div 10);
    c.s[i+j-1]:=c.s[i+j-1] mod 10;
    end;
  len:=a.len+b.len+1;
  {
    the product of a number with i digits and a number with j digits
    can only have at most i+j+1 digits
  }
  while(len>1)and(c.s[len]=0) do dec(len);
  c.len:=len;
end;

procedure main;
begin
  Multiply(x[1],x[2],y);
end;

procedure out;
begin
  assign(output,fn_out);
  rewrite(output);
  PrintHP(y);
  writeln;
  close(output);
end;

begin
  init;
  main;
  out;
end.
离线jimjim168
只看该作者 1 发表于: 2006-11-18
这个方法太癞,比它好的有的是!!!
快速回复
限100 字节
 
上一个 下一个