一、任意数的长度公式:给一个数X,len=log10(X)+1就是X这个数的长度 二、求N!有多少位。因为有斯特林公式,所以求n!的位数即
log10(n!)=log10(sqrt(2*acos(-1.0)*n))+n*log10(n/exp(1.0));
#include <iostream> #include <algorithm> #include<string.h> #include<stdio.h> #include<math.h> using namespace std; const double PI=3.1415926; int main() { int t,n,a; while(scanf("%d",&n)!=EOF) { a=(int)((0.5*log(2*PI*n)+n*log(n)-n)/log(10)); printf("%d\n",a+1); } return 0; } N!=1*2*3*4*5*............*N;如果要计算N!后得到的数字,则我们可以知道其等于lgN!+1lgN!=lg1+lg2+lg3+lg4+lg5+....................+lgN;但是当N很大的时候,我们可以通过数学公式进行优化:(即Stirling公式)N!=sqrt(2*pi*N)*(N/e)^N;(pi=3.1415926=acos(-1.0),e=2.718)lgN!=(lg(2*pi)+lgN)/2+N*(lgN-lge);