基准时间限制:1 秒 空间限制:131072 KB 分值: 0
难度:基础题
输入N求N的阶乘的10进制表示的长度。例如6! = 720,长度为3。
Input
输入N(1 <= N <= 10^6)
Output
输出N的阶乘的长度
Input示例
6
Output示例
3
一、任意数的长度公式:给一个数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!+1
lgN!=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);