HDU 1018 Big Number

来源:互联网 发布:拍照软件带时间 编辑:程序博客网 时间:2024/05/29 04:00

Problem Description
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.

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

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

Sample Input

2
10
20

Sample Output

7
19
题意:
给你一个数T,下面跟着T行,每一行一个数n,你需要输出n的阶乘的位数。
题解:
首先如果你硬着去算的话,必定会超出储存的限制,得到一个WA,这是也许你会想到用字符串来解决这个问题。但是题目的数据规模达到10的七次方。他的阶乘的位数是一个巨大的数。这时我们就要想另外的方法。我们在这里需要用到一个结论一个数a的位数x等于log10(a)。
下面我们来看一下这个结论的证明

  如果10^x-1<=a<10^x那么***a***的位数就是x,然后x-1<=log10(a)<x推导出x=(int)log10(a)+1。那么n的阶乘的位数就是log10(1*2*3*……*n)+1;由log10(a*b)=log10(a)+log10(b)。所以n的阶乘的位数就是(int)(log10(1)+log10(2)+.......log10(n))+1。有以上结论就可以开始写代码了
#include<stdio.h>#include<math.h>int main(){    int i,j,n;    scanf("%d",&n);    while(n--)    {        scanf("%d",&j);        double sum=0;        for(i=1;i<=j;i++)        {            sum+=log10(i);        }        int s        s=(int)sum;        s=s+1;        printf("%d\n",s);    }} 
0 0
原创粉丝点击