主题:求助:高精度算法一题!
用高精度算法计算N-1
program DEJC;
const
max=200;
type
arr=array[1..max] of 0..9;
var
a,b,c:arr;
n1,minans:string;
lena,lenc,i,x:integer;
input,output:text;
begin
assign(input,'input.in'); reset(input);
assign(output,'output.out'); rewrite(output);
readln(input,n1);
close(input);
lena:=length(n1);
for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord('0');
b[1]:=1;
i:=1;
while (i<=lena)or (i<=1) do
begin
x:=a[i]-b[i]+10+x;
c[i]:=x mod 10;
x:=x div 10 - 1;
i:=i+1;
end;
lenc:=i;
while (c[lenc]=0) and (lenc>1) do dec(lenc);
for i:=lenc downto 1 do minans[lenc-i+1]:=chr(c[i]+ord('0'));
write(output,minans);
close(output);
end.
根本不显示结果,如果将for i:=lenc downto 1 do minans[lenc-i+1]:=chr(c[i]+ord('0'));
write(output,minans);
close(output);改为
for i:= lenc downto 1 do write(output,c[i]);
writeln(output,'');
close(output);
则有效,什么原因?如果改正啊?
program DEJC;
const
max=200;
type
arr=array[1..max] of 0..9;
var
a,b,c:arr;
n1,minans:string;
lena,lenc,i,x:integer;
input,output:text;
begin
assign(input,'input.in'); reset(input);
assign(output,'output.out'); rewrite(output);
readln(input,n1);
close(input);
lena:=length(n1);
for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord('0');
b[1]:=1;
i:=1;
while (i<=lena)or (i<=1) do
begin
x:=a[i]-b[i]+10+x;
c[i]:=x mod 10;
x:=x div 10 - 1;
i:=i+1;
end;
lenc:=i;
while (c[lenc]=0) and (lenc>1) do dec(lenc);
for i:=lenc downto 1 do minans[lenc-i+1]:=chr(c[i]+ord('0'));
write(output,minans);
close(output);
end.
根本不显示结果,如果将for i:=lenc downto 1 do minans[lenc-i+1]:=chr(c[i]+ord('0'));
write(output,minans);
close(output);改为
for i:= lenc downto 1 do write(output,c[i]);
writeln(output,'');
close(output);
则有效,什么原因?如果改正啊?