【基础】高精度减法,同高精度加法

来自本人百度空间 2009-08-20 16:41

program gaojingdu(input,output);
var
        str1,str2:string;
        a1,a2:array[1..10000] of 0..9;
        bol:boolean;
        i,l,a:longint;
procedure init(a:integer);
var     j:longint;
begin
        if a=1 then begin
            for i:=length(str1) downto 1 do a1[length(str1)-i+1]:=ord(str1[i])-ord('0');
            for i:=length(str2) downto 1 do a2[length(str2)-i+1]:=ord(str2[i])-ord('0');
            bol:=true;
            l:=length(str1);
        end else begin
            for i:=length(str2) downto 1 do a1[length(str2)-i+1]:=ord(str2[i])-ord('0');
            for i:=length(str1) downto 1 do a2[length(str1)-i+1]:=ord(str1[i])-ord('0');
            bol:=false;
            l:=length(str2);
        end;
end;{init}
begin
        assign(input,'gaojingdu.in');
        reset(input);
        assign(output,'gaojingdu.out');
        rewrite(output);

        readln(str1);
        readln(str2);
        close(input);

        fillchar(a1,sizeof(a1),0);
        fillchar(a2,sizeof(a2),0);

        if length(str1)=length(str2) then
                if str1[1]>str2[1] then init(1)
                    else init(2)
            else if length(str1)>length(str2) then init(1)
            else init(2);
        if not(bol) then write('-');
        for i:=1 to l do begin
           if a1[i]<a2[i] then begin
               dec(a1[i+1]);
               a1[i]:=a1[i]+10;
           end;
           a1[i]:=a1[i]-a2[i];
        end;
        bol:=true;
        for i:=l downto 1 do
            if bol and (a1[i]=0) then
               else if bol and (a1[i]<>0) then begin
                   write(a1[i]);
                   bol:=false;
               end
               else write(a1[i]);
        close(output);
end.

小弟编出来就没简化,见谅~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值