HDOJ 1018(阶乘位数)

1.log10(num)+1

 

2.较小数情况下,sprintf(str,"%d",num);输出strlen(str),所以不能初始化为0

 

自然对数:log()  ,以二为底log2(num)=log(num)/log2,因为那个自然对数的底是无理数,不好表示,只要有了自然对数的表示,其他用换底公式就OK啦

 

 

 

我一直想的是求出阶乘后,再strlen(str),其实求阶乘位数有一个公式

 

 

 

//lnN!=NlogN-N+0.5*log(2*PI*N)

 

 

#include<stdio.h>

#include<math.h>

#define PI 3.14159265

int num,ans;

void count_factorial()

{

    double t;

    t = (num*log(num) - num + 0.5*log(2*num*PI))/log(10);

    ans= (int)t+1;

    printf("%d\n",ans);

}

int main()

{

    int i,n;

    scanf("%d",&n);

    for( i=1 ; i<=n ; i++ )  

    {

scanf("%d",&num);

count_factorial();

}

return 0;

}

 

3.

posted @ 2012-04-21 14:53  加拿大小哥哥  阅读(415)  评论(0编辑  收藏  举报