求1000的阶乘 ( 积分: 100 )

  • 主题发起人 主题发起人 2tt1314
  • 开始时间 开始时间
2

2tt1314

Unregistered / Unconfirmed
GUEST, unregistred user!
求1000的阶乘,哪位高手公布一下如何算出来的。万分感谢!
 
VAR
i,n:integer;
p : real;
begin
n := 1000;
p := 1;
for i := 1 to n do
begin
p := i*p;
end;

end;
 
楼上的.这样算会溢出的...
 
VAR
i,n:integer;
p : Extended;
begin
n := 1000;
p := 1;
for i := 1 to n do
begin
p := i*p;
end;
showmessage(floattostr(p));
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1311899
http://www.delphibbs.com/delphibbs/dispq.asp?lid=494104
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
Edit2: TEdit;
Label2: TLabel;
Memo1: TMemo;
Edit3: TEdit;
Label3: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation

{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Arr: array[0..100000] of integer;
N,i,k,c,m,fag:integer;
begin
edit2.Text:='';
N:=strtoint(edit1.Text);
arr[0]:=1;
arr[1]:=1;
if (N=0)or(N=1) then
edit2.Text:='1'
else
begin
for K:=2 to N do
begin
m:=arr[0];
c:=0;
for i:=1 to m do
begin
fag:=arr;
arr:=(arr*k+c) mod 10;
c:=(fag*k+c) div 10;
end;
while c>0 do
begin
m:=m+1;
arr[m]:=c mod 10;
c:=c div 10;
end;
arr[0]:=m;
end;

for i:=1 to m do
edit2.Text:=inttostr(arr)+edit2.Text;
end;
edit3.Text:=inttostr(arr[0]);
memo1.Lines.Add(edit1.Text+' 的阶乘为:');
memo1.Lines.Add(edit2.Text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.Text:='';
edit2.Text:='';
edit3.Text:='';
memo1.Lines.Clear;
end;
end.
//************************
这个是刚才自己没事做来玩玩的,算法很简单,采用一个整形数组arr[0..maxsz]存放
arr[0]记录长度....
 
记得到172的阶乘时开始仪溢出。。。。

用字符串模拟乘法吧 以前用VB写过
 
估算一下1000!有多大,LZ你真是无畏啊
 
求n的阶乘
function NFactorial(n: integer): integer;
begin
if n = 1 then
Result := 1
else
Result := NFactorial(n - 1) * n;
end;
procedure TForm1.Button1click(Sender: TObject);
var
itemp: integer;
begin
itemp := NFactorial(1000);//求1000!
end;
 
真是....无畏啊!这么大的天文数字!

方法1:
function Fact(n:Integer):string;
var
s:String;
m,i,C,nn:Integer;
begin
if (n<=1) then
begin
Result:='1';
exit;
end;
s:=Fact(n-1);
m:=length(s);
Result:='';
C:=0;
for i:=1 to m do
begin
nn:=StrToInt(Copy(s,m+1-i,1))*n+C;
C:=(nn div 10);
nn:=(nn mod 10);
Result:=IntToStr(nn)+Result;
end;
if C>0 then Result:=IntToStr(C)+Result;
end;


procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to 100 do
Memo1.lines.Add(Format('%3d!=%s',[i,Fact(i)]));
end;

方法2:
function f_factorial(int_n:integer): string;
var
i:integer ;
ls_result : string ;
begin
ls_result := inttostr(int_n) ;
for i:=int_n-1 downto 1 do
begin
ls_result := f_large_mly(ls_result,inttostr(i)) ;
end ;
result := ls_result ;
end ;


//兩大數相加
function f_large_add(str_m, str_n: string): string;
var
i, j, li_m, li_n, li_mid, li_mid_pre: integer;
ls_m, ls_n, ls_result, ls_mid, ls_mid_pre, ls_1, ls_2: string;
begin
ls_m := trim(str_m);
ls_n := trim(str_n);
li_m := length(ls_m);
li_n := length(ls_n);
if li_m > li_n then
begin
for i := 1 to li_m - li_n do
begin
ls_n := '0' + ls_n;
end;
li_n := li_m;
end
else begin
for i := 1 to li_n - li_m do
begin
ls_m := '0' + ls_m;
end;
li_m := li_n;
end;
ls_result := '';
ls_mid_pre := '100';
for i := 1 to li_m do
begin
ls_1 := copy(ls_m, li_m - i + 1, 1);
ls_2 := copy(ls_n, li_n - i + 1, 1);
li_mid := 100 + strtoint(ls_1) + strtoint(ls_2);
ls_mid := inttostr(li_mid + strtoint(copy(ls_mid_pre, 2, 1)));
ls_result := copy(ls_mid, 3, 1) + ls_result;
ls_mid_pre := ls_mid;
end;
if copy(ls_mid_pre, 2, 1) <> '0' then
begin
ls_result := copy(ls_mid_pre, 2, 1) + ls_result;
end;
result := ls_result;
end;

//兩大數相乘
function f_large_mly(str_m, str_n: string): string;
var
ls_m, ls_n, ls_mid, ls_mid_gw, ls_result, ls_zero: string;
i, j, li_m, li_n, li_m_bit, li_n_bit, li_mid, li_mid_pre, li_mid_gw, li_zero: integer;
begin
ls_m := trim(str_m);
ls_n := trim(str_n);
li_m := length(ls_m);
li_n := length(ls_n);
ls_result := '';
for j := 1 to li_n do
begin
li_mid_pre := 100;
li_mid := 0;
ls_mid := '';
for i := 1 to li_m do
begin
li_mid := 100 + strtoint(copy(ls_m, li_m - i + 1, 1)) * strtoint(copy(ls_n, li_n - j + 1, 1));
ls_mid_gw := trim(inttostr(100 + strtoint(copy(trim(inttostr(li_mid)), 3, 1)) + strtoint(copy(trim(inttostr(li_mid_pre)), 2, 1))))
//個位=本次個位+上次十位數字
ls_mid := copy(ls_mid_gw, 3, 1) + ls_mid;
li_mid_pre := li_mid + 10 * (strtoint(copy(ls_mid_gw, 2, 1)));
end;
if copy(inttostr(li_mid_pre), 2, 1) <> '0' then
begin
ls_mid := copy(inttostr(li_mid_pre), 2, 1) + ls_mid;
end;
ls_zero := '';
for li_zero := 1 to j - 1 do
begin
ls_zero := ls_zero + '0';
end;
ls_result := f_large_add(ls_result, ls_mid + ls_zero)
//兩大數相加
end;
result := ls_result;
end;
==============================================================================
自己看阶乘数:


0!=1
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
10!=3628800
11!=39916800
12!=479001600
13!=6227020800
14!=87178291200
15!=1307674368000
16!=20922789888000
17!=355687428096000
18!=6402373705728000
19!=121645100408832000
20!=2432902008176640000
21!=51090942171709440000
22!=1124000727777607680000
23!=25852016738884976640000
24!=620448401733239439360000
25!=15511210043330985984000000
26!=403291461126605635584000000
27!=10888869450418352160768000000
28!=304888344611713860501504000000
29!=8841761993739701954543616000000
30!=265252859812191058636308480000000
31!=8222838654177922817725562880000000
32!=263130836933693530167218012160000000
33!=8683317618811886495518194401280000000
34!=295232799039604140847618609643520000000
35!=10333147966386144929666651337523200000000
36!=371993326789901217467999448150835200000000
37!=13763753091226345046315979581580902400000000
38!=523022617466601111760007224100074291200000000
39!=20397882081197443358640281739902897356800000000
40!=815915283247897734345611269596115894272000000000
41!=33452526613163807108170062053440751665152000000000
42!=1405006117752879898543142606244511569936384000000000
43!=60415263063373835637355132068513997507264512000000000
44!=2658271574788448768043625811014615890319638528000000000
45!=119622220865480194561963161495657715064383733760000000000
46!=5502622159812088949850305428800254892961651752960000000000
47!=258623241511168180642964355153611979969197632389120000000000
48!=12413915592536072670862289047373375038521486354677760000000000
49!=608281864034267560872252163321295376887552831379210240000000000
50!=30414093201713378043612608166064768844377641568960512000000000000
51!=1551118753287382280224243016469303211063259720016986112000000000000
52!=80658175170943878571660636856403766975289505440883277824000000000000
53!=4274883284060025564298013753389399649690343788366813724672000000000000
54!=230843697339241380472092742683027581083278564571807941132288000000000000
55!=12696403353658275925965100847566516959580321051449436762275840000000000000
56!=710998587804863451854045647463724949736497978881168458687447040000000000000
57!=40526919504877216755680601905432322134980384796226602145184481280000000000000
58!=2350561331282878571829474910515074683828862318181142924420699914240000000000000
59!=138683118545689835737939019720389406345902876772687432540821294940160000000000000
60!=8320987112741390144276341183223364380754172606361245952449277696409600000000000000
61!=507580213877224798800856812176625227226004528988036003099405939480985600000000000000
62!=31469973260387937525653122354950764088012280797258232192163168247821107200000000000000
63!=1982608315404440064116146708361898137544773690227268628106279599612729753600000000000000
64!=126886932185884164103433389335161480802865516174545192198801894375214704230400000000000000
65!=8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
66!=544344939077443064003729240247842752644293064388798874532860126869671081148416000000000000000
67!=36471110918188685288249859096605464427167635314049524593701628500267962436943872000000000000000
68!=2480035542436830599600990418569171581047399201355367672371710738018221445712183296000000000000000
69!=171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000
70!=11978571669969891796072783721689098736458938142546425857555362864628009582789845319680000000000000000
71!=850478588567862317521167644239926010288584608120796235886430763388588680378079017697280000000000000000
72!=61234458376886086861524070385274672740778091784697328983823014963978384987221689274204160000000000000000
73!=4470115461512684340891257138125051110076800700282905015819080092370422104067183317016903680000000000000000
74!=330788544151938641225953028221253782145683251820934971170611926835411235700971565459250872320000000000000000
75!=24809140811395398091946477116594033660926243886570122837795894512655842677572867409443815424000000000000000000
76!=1885494701666050254987932260861146558230394535379329335672487982961844043495537923117729972224000000000000000000
77!=145183092028285869634070784086308284983740379224208358846781574688061991349156420080065207861248000000000000000000
78!=11324281178206297831457521158732046228731749579488251990048962825668835325234200766245086213177344000000000000000000
79!=894618213078297528685144171539831652069808216779571907213868063227837990693501860533361810841010176000000000000000000
80!=71569457046263802294811533723186532165584657342365752577109445058227039255480148842668944867280814080000000000000000000
81!=5797126020747367985879734231578109105412357244731625958745865049716390179693892056256184534249745940480000000000000000000
82!=475364333701284174842138206989404946643813294067993328617160934076743994734899148613007131808479167119360000000000000000000
83!=39455239697206586511897471180120610571436503407643446275224357528369751562996629334879591940103770870906880000000000000000000
84!=3314240134565353266999387579130131288000666286242049487118846032383059131291716864129885722968716753156177920000000000000000000
85!=281710411438055027694947944226061159480056634330574206405101912752560026159795933451040286452340924018275123200000000000000000000
86!=24227095383672732381765523203441259715284870552429381750838764496720162249742450276789464634901319465571660595200000000000000000000
87!=2107757298379527717213600518699389595229783738061356212322972511214654115727593174080683423236414793504734471782400000000000000000000
88!=185482642257398439114796845645546284380220968949399346684421580986889562184028199319100141244804501828416633516851200000000000000000000
89!=16507955160908461081216919262453619309839666236496541854913520707833171034378509739399912570787600662729080382999756800000000000000000000
90!=1485715964481761497309522733620825737885569961284688766942216863704985393094065876545992131370884059645617234469978112000000000000000000000
91!=135200152767840296255166568759495142147586866476906677791741734597153670771559994765685283954750449427751168336768008192000000000000000000000
92!=12438414054641307255475324325873553077577991715875414356840239582938137710983519518443046123837041347353107486982656753664000000000000000000000
93!=1156772507081641574759205162306240436214753229576413535186142281213246807121467315215203289516844845303838996289387078090752000000000000000000000
94!=108736615665674308027365285256786601004186803580182872307497374434045199869417927630229109214583415458560865651202385340530688000000000000000000000
95!=10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000
96!=991677934870949689209571401541893801158183648651267795444376054838492222809091499987689476037000748982075094738965754305639874560000000000000000000000
97!=96192759682482119853328425949563698712343813919172976158104477319333745612481875498805879175589072651261284189679678167647067832320000000000000000000000
98!=9426890448883247745626185743057242473809693764078951663494238777294707070023223798882976159207729119823605850588608460429412647567360000000000000000000000
99!=933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000
100!=93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000


1000!=402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
 
zqw0117不就是在用字符串模拟吗?

该给分了吧
 
用一个数组,字符串模拟一下,就出来了!
参加ACM时,这样的问题,很简单的!
 
谢谢大家了..

忘记给分了..郁闷.
 
后退
顶部