洛谷 2142高精度减法

来源:互联网 发布:sql沉思录 编辑:程序博客网 时间:2024/06/05 16:17

题目

高精度减法

两个数(第二个可能比第一个大)

结果(是负数要输出负号)

题解

高精度减法

代码

var  a,b,c:array[1..1000]of longint;  i,j,n:longint;  s,t,k:ansistring;procedure sub;var  i,j,k,x:longint;begin  i:=1;  while i<=length(s) 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];      inc(i);    end;end;begin  readln(s);  readln(t);  if (length(s)<length(t))or((length(s)=length(t))and(s<t)) then    begin      k:=s;s:=t;t:=k;      write('-');    end;  for i:=1 to length(s) do    a[length(s)-i+1]:=ord(s[i])-ord('0');  for i:=1 to length(t) do    b[length(t)-i+1]:=ord(t[i])-ord('0');  sub;  i:=1000;  while (c[i]=0)and(i>1) do dec(i);  for j:=i downto 1 do    write(c[j]);end.
1 0