斯特林公式在 ACM中的使用

*斯特林公式:

*求阶乘的第(1~14)位:

frac(n):取n的小数部分;

这个式子精确度比较高,精确度达到第14位;不过前几个数字需要手动打表算出,因为当n较小时,答案不符合;

详细的推导在链接:https://share.weiyun.com/df75bafcd0832c034b22fe2355a86a31 (密码:CWPP)(感谢作者)

*求阶乘的位数:


最后计算所得结果+1;



第一个题,求阶乘的第二位。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
typedef long long ll;

int main()
{
//    printf("%lf\n",pi);
    int m;
    int T[11]={0,0,0,0,4,2,2,0,0,6,6};
    scanf("%d",&m);
    while(m--)
    {
        double n;
        double s;
        scanf("%lf",&n);
        if(n<11)
        {
            printf("%d\n",T[(int)n]);
            continue;
        }
        s=0.5*log10(2*n*pi)+n*log10(n)-n*(log10(e));
        s=s-(int)s;
        s=pow(10,s);
        s=s*exp(1.0/12.0/n-1.0/360.0/n/n/n);
        printf("%d\n",((int)(s*10))%10);
    }
    return 0;
}


第二题:判断阶乘的位数。

#include<cstdio>
#include<cstring>
#include<cmath>
#define pi acos(-1.0)
#define e exp(1.0)//exp(x)是求e的x次方
int main()
{
    int t,n;
    double ans;
    while(~scanf("%d",&n))
    {
        ans=log10(sqrt(2*pi*n))+n*log10(n/e);
        printf("%d\n",(int)ans+1);
    }
    return 0;
}

//over!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值