主题:一个求n!的高精度值
program p12 ;
const m=200;
var a:array[1..m] of integer;
l,i,j,y,c,n,k,t:integer;f:real;
st1:string;
begin
write('input a number n:');
read(n);writeln;f:=0;
for i:=1 to n do
f:=f+ln(i)/ln(10);
t:=trunc(f)+1;
a[1]:=1;
l:=1;k:=1;
for i:=1 to n do
begin
c:=0;
while a[k]=0 do k:=k+1;
for j:=k to 1 do
begin
y:=a[j]*i+c;
c:=y div 1000;
a[j]:=y mod 1000
end;
if c>0 then begin
l:=l+1;
a[l]:=c;
end
end;
write(n,'!=');
for i:=l downto 1 do
begin
if i=1 then write(a[i])
else begin
str(a[i],st1);
while length(st1)<3 do st1:='0'+st1;
write(st1);
end;
end;
writeln;
end.
以上是完整的程序代码(摘自吴再陵版的《全国青少年信息学奥林匹克联赛》)但是运行完后,只能计算小于7的阶乘,若大于7的数,显示的结果就出错了
const m=200;
var a:array[1..m] of integer;
l,i,j,y,c,n,k,t:integer;f:real;
st1:string;
begin
write('input a number n:');
read(n);writeln;f:=0;
for i:=1 to n do
f:=f+ln(i)/ln(10);
t:=trunc(f)+1;
a[1]:=1;
l:=1;k:=1;
for i:=1 to n do
begin
c:=0;
while a[k]=0 do k:=k+1;
for j:=k to 1 do
begin
y:=a[j]*i+c;
c:=y div 1000;
a[j]:=y mod 1000
end;
if c>0 then begin
l:=l+1;
a[l]:=c;
end
end;
write(n,'!=');
for i:=l downto 1 do
begin
if i=1 then write(a[i])
else begin
str(a[i],st1);
while length(st1)<3 do st1:='0'+st1;
write(st1);
end;
end;
writeln;
end.
以上是完整的程序代码(摘自吴再陵版的《全国青少年信息学奥林匹克联赛》)但是运行完后,只能计算小于7的阶乘,若大于7的数,显示的结果就出错了