大数阶乘的位数

杭电1018Realtime Status

Problem Description

In many applications verylarge integers numbers are required. Some of these applications are using keysfor secure transmission of data, encryption, etc. In this problem you are givena number, you have to determine the number of digits in the factorial of thenumber.

Input

Input consists of severallines of integer numbers. The first line contains an integer n, which is thenumber of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107on each line.

Output

The output contains the numberof digits in the factorial of the integers appearing in the input.

Sample Input

2

10

20

Sample Output

7

19

 

1;介绍题目大意;

contains the number of digits……是说这个数的位数是多少的意思

factoria……………………………阶乘的意思;

大意就是求一个小于10^7方的大数,该数阶乘的位数是多少;

2;看到题目是不是很方;跟我一样;然而解答却十分吓人;

介绍一个公式;

……任意一个正整数a的位数都等于(int)log10(a) + 1;//关键;

很惊讶吧;让我来推导一遍吧;

10^(x-1)  <= a  <  10^x;……可以看出x就是a的位数吧;

…log10(10^(x-1))  <= log10(a)  < log10(10^x);

……x-1 <=  log10(a)  <  x;

………………(int)log10(a)  =  x-1;

……………………  x  = (int)log10(a) +1;

公式就介绍到这里;在看看这题与我们公式有什么关系呢;

题目是求n的阶乘的位数;

我们公式是求的a的位数;

然而我们难道不鞥吧a ===== n!;一切都明目了吧;

还有一个小公式,不知道大家还记得吗;

Log10(1*2*3*4*5*6*…………*n)

                                      =log10(1)+ log10(2)+…………log10(n)

现在我们就可以轻松ac了;开始敲代码吧;

#include<stdio.h>

#include<math.h>

int main()

{

    long long int n, a, i;

    double num;

    scanf("%lld",&n);

    while(n--){

        num = 0;

        scanf("%lld",&a);

        for(i = 1; i <= a;i++){

            num += log10(i*1.0);

        }

        printf("%d\n",(int)num+1);

    }

   

    return 0 ;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值