您的位置:首页 > 其它

hdu1141 (Factstone Benchmark(利用对数进行大数比较))

2015-01-22 19:10 330 查看
这个题的意思就是求 N!<2^bit

两边同时取对数,得到 log(N!)<bit*log(2.0),变换形式得到 log(N!)/ log(2.0)<bit;

log(N!)=log(1*2*3*4*……N)=log(1)+log(2)+log(3)+log(4)+……log(N);

则问题转化为求: log(1)/ log(2.0)+ log(2)/ log(2.0)+ log(3)/ log(2.0)+…… log(N)/ log(2.0)<bit

从而避免了大数计算,利用对数直接比较,将问题简单化

#include<stdio.h>
#include<math.h>
int main()
{
    int n,i;
    __int64 t;
    double ans;
    while(scanf("%d",&n),n!=0)
    {
        t=(n-1960)/10+2;
        t=1<<t;
        i=0;
        ans=0;
        while(ans<t)
        {
            ans+=log((double)(++i))/log(2.0);<span style="color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">//特别注意 i = 1 ,log(i++)不知道为什么错了 只能i = 0,log(++i)</span>
        }
        printf("%d\n",i-1);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: