请教一下关于高精度减法的问题
pascal吧
全部回复
仅看楼主
level 1
冰柩之城 楼主
如题,在编一个求任意两个正整数a,b的高精度减法程序,
Alt+F9显示编译成功,自己输数字进去调试发现有时候对有时候不对,
比如我输100和99,它显示错误代码201。。
以下发我写的程序,请高手赐教是哪里出问题了Orz
2013年11月21日 14点11分 1
level 1
冰柩之城 楼主
const n=5000;
var
str1,str2,str:ansistring;
a,b:array[1..n]of integer;
len1,len2,len,i,j:integer;
tf:boolean;
begin
readln(str1); readln(str2);
len1:=length(str1); len2:=length(str2);
if str1=str2 then
begin
write('0');
halt;
end;
if len1>len2 then
tf:=true
else
if len1<len2 then
tf:=false
else
begin
i:=1; tf:=true; len:=len1;
while (len>0) and tf do
begin
if str1[i]>str2[i] then break;
if str1[i]<str2[i] then
begin
tf:=false;
break;
end;
inc(i); dec(len);
end;
end;
if not tf then
begin
str:=str1; str1:=str2; str2:=str;
len:=len1; len1:=len2; len2:=len;
end;
for i:=1 to len1 do a[i]:=ord(str1[len1-i+1])-48;
for i:=1 to len2 do b[i]:=ord(str2[len1-i+1])-48;
for i:=1 to len1 do
begin
if a[i]<b[i] then
begin
dec(a[i+1]);
inc(a[i],10);
end;
dec(a[i],b[i]);
end;
while a[len1]=0 do dec(len1);
if not tf then write('-');
for i:=len1 downto 1 do write(a[i]);
readln;
end.
2013年11月21日 14点11分 2
level 6
program sub;
var
a, b: array[1..10000] of longint;
st1, st2, t: ansistring;
i, j, len: longint;
begin
Assign(input, 'sub.in');
reset(input);
Assign(output, 'sub.out');
rewrite(output);
readln(st1);
readln(st2);
len := length(st1);
if (len < length(st2)) or ((len = length(st2)) and (st1 < st2)) then
begin
len := length(st2);
Write('-');
t := st1;
st1 := st2;
st2 := t;
end;
j := 1;
for i := 1 to len do
begin
a[i] := Ord(st1[j]) - 48;
Inc(j);
end;
j := 1;
for i := len - length(st2) + 1 to len do
begin
b[i] := Ord(st2[j]) - 48;
Inc(j);
end;
for i := len downto 1 do
begin
a[i] := a[i] - b[i];
if a[i] < 0 then
begin
Dec(a[i - 1]);
Inc(a[i], 10);
end;
end;
j := 1;
while (a[j] = 0) and (j < len) do
Inc(j);
for i := j to len do
Write(a[i]);
writeln;
Close(input);
Close(output);
end.
2013年11月22日 10点11分 3
正解
2013年11月22日 10点11分
回复 ijpoj :嗯明白了多谢!
2013年11月22日 15点11分
1