首页 > 代码库 > HDU 1018

HDU 1018

题意:求n!的位数

题解:斯特林数: log10(n!)=1.0/2*log10(2*pi*n)+n*log10(n/e)。

 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #define E 2.71828182 6 #define PI acos( -1.0 ) 7  8 int n; 9 10 int main()11 {12     int T;13     scanf( "%d", &T );14     while( T-- )15     {16         scanf( "%d", &n );17         double sum = (double)n*log10( n / E ) + 0.5*log10( 2.0*PI*n );18         printf( "%d\n", (int)sum+1 );19     }20     return 0;21 }
View Code

 

当然这题也可以用暴力。

 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5  6 int n; 7  8 int main() 9 {10     int T;11     scanf( "%d", &T );12     while( T-- )13     {14         scanf( "%d", &n );15         double sum = 0;16         for( int i = 1; i <= n; ++i )17             sum += log10( (double)i );18         printf( "%d\n", (int)sum+1 );19     }20     return 0;21 }
View Code

 

HDU 1018