viết chương trình con : phép nhân 2 số lớn (Bignum)

By Kinsley

viết chương trình con : phép nhân 2 số lớn (Bignum)

0 bình luận về “viết chương trình con : phép nhân 2 số lớn (Bignum)”

  1. Cách khởi tạo Bignum:

    type bignum = array [0..maxn] of word;
    var     xs,ys:ansistring;
            x,y,z:bignum;
            i,j,dem,max:longint;
    begin
            readln(xs);
            readln(ys);
            fillchar(x,sizeof(x),0);
            fillchar(y,sizeof(y),0);
            x[0]:=length(xs);
            y[0]:=length(ys);
            for i:=x[0] downto 1 do
                  begin  x[x[0]-i+1]:= ord(xs[i])-48;   end;
            for j:=y[0] downto 1 do
                  begin  y[y[0]-j+1]:= ord(xs[j])-48;   end;

    end.

    Ý tưởng: a x b = a + a + a … a (b lần)

    (mình chỉ có sẵn thuật toán cộng số lớn, còn lại bạn tự viết nha)

    const maxn=1000;
    type bignum = array [0..maxn] of word;
    var     xs,ys:ansistring;
            x,y,z:bignum;
            i,j,dem,max:longint;
    begin
            readln(xs);
            readln(ys);
            fillchar(x,sizeof(x),0);
            fillchar(y,sizeof(y),0);
            fillchar(z,sizeof(z),0);
            x[0]:=length(xs);
            y[0]:=length(ys);
            for i:=x[0] downto 1 do
                  begin  x[x[0]-i+1]:= ord(xs[i])-48;   end;
            for j:=y[0] downto 1 do
                  begin  y[y[0]-j+1]:= ord(xs[j])-48;   end;
            dem:=0;
            if x[0] > y[0] then max:= x[0] else max:=y[0];
            for i:=1 to max do
                    begin
                         z[i]:=(x[i]+y[i]+dem) mod 10;
                         dem:=(x[i]+y[i]+dem) div 10;
                    end;
            if dem >0 then
            begin
                    inc(z[0]);
                    z[z[0]]:=dem;
            end;
            for i:=max downto 1 do
                    write(z[i]);
    end.

    Trả lời

Viết một bình luận