pascal求10000以内n的阶乘?Program jiecheng ;var s,n:longint;function fac(n:integer):integer;vark,t:integer;begint:=1;for k:=2 to n dot:=t*k;fac:=t;end;beginwriteln('Input n:');read(n);s:=fac(n);write('s=',s);end.由于是新手,刚接触pascal,
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/23 14:02:01
pascal求10000以内n的阶乘?Program jiecheng ;var s,n:longint;function fac(n:integer):integer;vark,t:integer;begint:=1;for k:=2 to n dot:=t*k;fac:=t;end;beginwriteln('Input n:');read(n);s:=fac(n);write('s=',s);end.由于是新手,刚接触pascal,
pascal求10000以内n的阶乘?
Program jiecheng ;
var s,n:longint;
function fac(n:integer):integer;
var
k,t:integer;
begin
t:=1;
for k:=2 to n do
t:=t*k;
fac:=t;
end;
begin
writeln('Input n:');
read(n);
s:=fac(n);
write('s=',s);
end.
由于是新手,刚接触pascal,所以很多东西都不懂.
怎么样使n≤10000;
原来编的内容里,运行的时候如果输入一个较大的数就会显示20l
201范围检查错是什么意思?怎么改额?
pascal求10000以内n的阶乘?Program jiecheng ;var s,n:longint;function fac(n:integer):integer;vark,t:integer;begint:=1;for k:=2 to n dot:=t*k;fac:=t;end;beginwriteln('Input n:');read(n);s:=fac(n);write('s=',s);end.由于是新手,刚接触pascal,
目前几乎没有算法实现10000阶乘
201错误是数值溢出
integer的范围是-32767..32767,用作阶乘是远远不够的
简单递归只能算到12的阶乘:
Program jiecheng ;
var s,n:longint;
function fac(n:integer):LONGINT;
var
k,t:integer;
begin
t:=1k:=2 to n do
t:=t*k;
fac:=t;
end;
begin
writeln('Input n:');
read(n);
WHILE N>12 DO BEGIN
WRITELN('Error!);
READLN(N);
END//这一段可在输入超范围数时报错,输入范围内的数再继续
s:=fac(n);
write('s=',s);
end.
还有一个高精度阶乘的程序(大约可算到5000)
program HighPrecisionFactorial;
var hd:array[1..10000] of word;
a,i,j,k,c,t:integer;
fo:text;
St:STRING;
begin
c:=10;
readln(a);
hd[1]:=1;
k:=1;
for i:=2 to a do begin
t:=0;
j:=1;
repeat
hd[j]:=hd[j]*i;
hd[j]:=hd[j]+t;
t:=0;
if hd[j]>=c then begin
t:=hd[j] div c;
hd[j]:=hd[j] mod c;
if j=k then k:=j+1;
end;
inc(j);
until j>k;
end;
writeln(a,'!=');
for i:=k downto 1 do write(hd[i]);
end.