标签:
1 program exam2; 2 const 3 max=200; 4 var 5 a,b,c:array[1..max] of 0..9; 6 n,n1,n2:string; 7 lena,lenb,lenc,i,x:integer; 8 begin 9 readln(n1); 10 readln(n2); {处理被减数和减数} 11 if (length(n1)<length(n2)) or (length(n1)=length(n2)) and (n1<n2) then 12 begin 13 n:=n1;n1:=n2;n2:=n; 14 write(’-’) {n1<n2,结果为负数} 15 end; 16 lena:=length(n1); lenb:=length(n2); 17 for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord(’0’); 18 for i:=1 to lenb do b[lenb-i+1]:=ord(n2[i])-ord(’0’); 19 i:=1; 20 x:=0; 21 while (i<=lena) or(i<=lenb) do 22 begin 23 x := a[i] - b[i] + 10 + x; {不考虑大小问题,先往高位借10} 24 c[i] := x mod 10 ; {保存第i 位的值} 25 x := x div 10 - 1; {将高位借掉的1减去} 26 i := i + 1 27 end; 28 lenc:=i; 29 while (c[lenc]=0) and (lenc>1) do dec(lenc); {最高位的0 不输出} 30 for i:=lenc downto 1 do write(c[i]); 31 writeln 32 end.
标签:
原文地址:http://www.cnblogs.com/vacation/p/5022168.html