Bài toán tính hiệu số lớn

25 views
Skip to first unread message

tienph...@gmail.com

unread,
Feb 24, 2013, 10:46:30 PM2/24/13
to thcsthait...@googlegroups.com
Tính hiệu của hai số nguyên a và b (a,b<=10^100)

tienph...@gmail.com

unread,
Feb 24, 2013, 10:48:45 PM2/24/13
to thcsthait...@googlegroups.com, tienph...@gmail.com
var
        f:text;
        a,b:string;
procedure input(var a,b:string);
        var
                f:text;
        begin
                assign(f,'hieu.inp');
                reset(f);
                readln(f,a);
                read(f,b);
                close(f);
        end;  
procedure swap(var a,b:string);
var 
tam:string;
begin       
tam:=a;
a:=b;
b:=tam;
end;
Function hieu(a,b:string):string;
        var
                soa,sob,muon,code,i,ab:integer;
                snho,kq,sab,dau:string; 
                da,db:double;
        begin    
       
                while length(b)<length(a) do
                b:='0'+b;  
                while length(a)<length(b) do
                a:='0'+a;
                
                muon:=0; 
                kq:='';
                for i:=length(a) downto 1 do
                 Begin
                       val(a[i],soa,code);
                       val(b[i],sob,code);
soa:=soa-muon;
if soa>sob then
Begin
ab:=soa-sob;
muon:=0;
end
else
begin
soa:=soa+10;
muon:=1;
ab:=soa-sob;
end;
str(ab,sab);
kq:=sab+kq;
                                
                        end;
                if kq[1]='0' then delete(kq,1,1);
                
                hieu:=kq;
        end;
{Chuong trinh chinh}
Begin
        input(a,b);
        assign(f,'hieu.out');
        rewrite(f);

        if a<b then
        write(f,'-',hieu(b,a))
        else
        write(f,hieu(a,b));

       
        close(f);
End.
Reply all
Reply to author
Forward
0 new messages