Ask N. The number of digits
The main learning today is the Stirling formula:
log10 (sqrt (2.0*pi*n)) +n*log10 (n/e)
Problem Solving Ideas:
Two methods of solution
1:1+LG (1) +LG (2) +. +LG (N)
2:strling formula.
Method One:
Analysis:
The title means to give an n and ask how many digits the n!. For this topic, we first think of the use of high-precision multiplication method simulation, and finally look at the number of bits, but the problem of large data, up to 10^7, high-precision simulation obviously not. So we have to think differently.
To know what the number of digits of a number is, we can use the LOG10 function to calculate. For example, for a number, n=10,10!=3628800, and log10 (3628800) = 6.559763033, so long as the number is rounded up, is 7, is 10! The number of digits. Of course, direct calculation log10 (10!) is impossible, but according to the nature of the addition of logarithms, we have log10 (10!) = log10 (1*2*3.....*9*10) =log10 (1) + log10 (2) + log10 (3) + ... + log10 (9) +log10 (10), so , even 10^7, we can calculate the function of log10 directly. As for rounding up, we have another function whose prototype is double Ceil (double). With this function we can easily rounding up a floating-point number.
LOG10 (n!) is the number of result digits of the n!.
#include <math.h> #include <stdio.h> Const double Pi=acos (-1.0); made int wrong nearly five times Const Double E=EXP (1.0);
remember int main (int argc, char *argv[])
{int T;
int n,i;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n); if (n==1)//n=1 output 0 ... printf ("1\n")
else {int answer= (ceil) (log10 (2.0*pi*n)/2.0+n* (log10 (n/e)));
printf ("%d\n", answer);
}} return 0; }
To play the table method
#include <stdio.h>
int main ()
{
int n,m,i,bit=1;
Double num=0;
scanf ("%d", &n);
while (n--)
{
bit=1,num=1;
scanf ("%d", &m);
for (i=2;i<=m;i++)
{//num is only converted to an integer part with 1 bits, the remainder is in fractional parts, preventing method overflow
num*=i;
if (num<10) {continue;}
if (num<100) {num/=10;bit+=1;continue;}
if (num<1000) {num/=100;bit+=2;continue;}
if (num<10000) {num/=1000;bit+=3;continue;}
if (num<100000) {num/=10000;bit+=4;continue;}
if (num<1000000) {num/=100000;bit+=5;continue;}
if (num<10000000) {num/=1000000;bit+=6;continue;}
if (num<100000000) {num/=10000000;bit+=7;continue;}
}
printf ("%d\n", bit);
}
return 0;
}