source : "Algorithmic Competition Primer Classic" Example 5.2.2
title : Input not more than 1000 positive integer n, output n!=1*2*3*...*n accurate results.
Example input : 30
Sample Output : 265252859812191058636308480000000
analysis : To save the results, you need to analyze 1000! how big. It's not hard to figure out a calculator, 1000! Approximately equals 4*102567, so you can save it with an array of 3,000 elements buf. For convenience, we let f[0] Save the result of the bit, f[1] is 10 bits, f[2] is the hundred ... (Why do you want to show it in reverse order?) Because if you say it from the low order, once the rounding is up ... ), the n! can be completed at the same time only by simulating the hand count. You need to ignore the leading 0 in the output
Source :
#include <stdio.h>#include<string.h>Const intMAXN = the;intBUF[MAXN];intn_factorial () {intI,j,n,s,c; scanf ("%d",&N); memset (BUF,0,sizeof(BUF));//put the array f 0buf[0]=1; for(i=2; i<=n;i++)//cyclic multiplication I{C=0; for(j=0; j<maxn;j++)//each of them is multiplied by I (analog hand calculation){s= Buf[j] * i +C; BUF[J]= s%Ten;//keep in that bitc = s/Ten;//rounding up a bit } } /*Output Results*/ for(j=maxn-1; j>=0; j--) if(Buf[j]) Break;//Ignore leading 0 for(i=j;i>=0; i--) printf ("%d", Buf[i]); printf ("\ n"); return 0;}
Algorithm chapter-the exact value of factorial