由斯特林[striling]公式可得:
lnN!=NlnN-N+0.5ln(2N*pi)
而N!的位数m = ceil(log10(N!))+1
ceil函数取上界,即不小于某数的最小整数
log10(N!) = lnN!/ln10
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
static int striling(int m);
int main(int argc, char **argv)
{
int m, n, i;
int res[10];
scanf("%d", &n);
getchar();
for(i = 0; i < n; i++){
scanf("%d", &m);
getchar();
res[i] = striling(m);
}
for(i = 0; i < n; i++){
printf("%d\n", res[i]);
}
exit(0);
}
static int striling(int m)
{
int len;
if((m == 1) || (m == 0)){
return 1;
}
len = ceil((m*log(m)-m+(log(2*m*PI))/2)/log(10));
return len;
}
|
阅读(818) | 评论(0) | 转发(0) |