给两个斯特林公式的链接
http://episte.math.ntu.edu.tw/articles/mm/mm_17_2_05/page2.html
http://hi.baidu.com/vincentz/blog/item/54686c63fa113f630c33fafe.html
现在为止,遇到两个这样的题,还是写一下,尽管是水题。
第一个题,求阶乘的最高位。
http://acm.cs.ecnu.edu.cn/problem.php?problemid=1007
不用说,肯定是用斯特林公式,不过要注意前面1~9还是自己打个表吧,很简单的。
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define e 2.71828182845
#define pi 3.1415926
int main()
{
long a ;
double s ;
int result = 0 ;
while(scanf("%d", &a ) != EOF )
{
if ( a >3 && a != 7 && a != 8 )
{
s = 0.5 * log10 ( 2 * pi * a ) + a * ( log10 ( a ) - log10(e) ) ;
s = s - ( int ) s ;
result = ( int ) pow ( 10 , s ) ;
printf("%d\n",result ) ;
continue ;
}
if ( a <= 1 ) {printf("1\n"); continue ;}
if ( a == 2 ) {printf("2\n" ) ; continue ; }
if ( a ==3 ) { printf("6\n" ) ;continue ;}
if ( a == 7 ) {printf("5\n"); continue ; }
if ( a == 8 ) {printf("4\n" ) ;continue ; }
}
}
第二题:判断阶乘的位数。
http://poj.org/problem?id=1423
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
const double e = 2.71828182845;
const double pi = 3.1415926;
int main(void)
{
int t, i, f, v;
double a, s;
const double log10_e = log10(e);
const double log10_2_pi = log10(2.0*pi)/2.0;
while (scanf("%d", &t) != EOF && t)
{
for (i = 0; i < t; ++i)
{
scanf("%d\n", &v);
if (1 == v) {printf("1\n"); continue;}
a = v;
s = log10_2_pi + (a+0.5)*log10(a) - a * log10_e;
f = ceil(s);
printf("%d\n", f);
}
}
return 0;
}
1的时候不满足公式。