斯特林公式在 ACM中的使用

给两个斯特林公式的链接

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的时候不满足公式。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值