zoj 1526 Big Number (N阶乘的位数)

注:还是推公式,类似zoj2277,打表超内存(话说才10^7数组就爆了,也是醉了),默默暴力计算930ms险过


Big Number

Time Limit: 10 Seconds       Memory Limit: 32768 KB

In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.


题意:求n!的位数

设  n!= a * 10^k  ( 1<=a<10 )

(1*2*3*4*...*n)  = a * 10^k

两边对10取对数: log10(1*2*3*4*...*n) = log10(a * 10^k)

log10(1)+log10(2)+log10(3)+log10(4)+...+log10(n) = log10(a)+k

因为 0<=log10(a)<1

所以将左边数加起来,再向下取整,结果就是k

最后结果为k+1 (10^0 =1)


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define N 10000010
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        double sum=0.0,ans;
        for(int i=1; i<=n; i++){
            sum=sum+log10(i*1.0);
        }
        ans=floor(sum);
        printf("%.0f\n",ans+1.0);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值