Law one: The logarithm of a number, +1 rounding is its number of digits
Problem converted to int (log10 (n!) +1), logarithmic property log10 (n!) =LOG10 (N) +log10 (N-1) +...+log10 (1)
/* Use LOG10 to find the number of digits */#include <stdio.h> #include <math.h>int main () { int tim,n; scanf ("%d", &tim); while (tim--) { int i; Double numofdigit=1; scanf ("%d", &n); for (i=n;i>=1;i--) { numofdigit+=log10 (i); } printf ("%d\n", (int) numofdigit);} }
When n is large, time is long, TLE
Law II: Stirling formula
Log (n!) = log10 (sqrt (2*pi*n)) + N*LOG10 (n/e);
/* Use Stirling to find the number of positions */#include <stdio.h> #include <math.h> #define e 2.718281828459045#define pi 3.141592653589793239int Main () { int t; scanf ("%d", &t); while (t--) { int n; scanf ("%d", &n); Double num_digit; Num_digit=log10 (sqrt (2*pi*n)) + N*LOG10 (n/e); printf ("%d\n", (int) num_digit+1); } return 0;}
WA two times reason:
Num_digit=log10 (sqrt (2*pi*n)) + n*log10 (n/e) +1; printf ("%d\n", (int) num_digit);
When N=1,num_digit=0, because when N=1,
log10 (sqrt (2*pi*n)) + n*log10 (n/e) =-0.03
The last value is 0.
Summary: When using the Stirling formula to calculate the number of digits, taking into account the n=1, plus 1 placed after taking the whole
(int) (Log10 (sqrt (2*pi*n)) + N*LOG10 (n/e)) =0
Plus 1, after taking the whole place.
Int (3.1+1) =4
Int (3.1) +1=4
Int (3) +1=4
Int (3+1) =4
Int (-0.03) +1=1
poj1423---Ask for a large number of digits, I guess the algorithm that counts less than how many bits of input characters on the site