HDU 1018

B - Big Number
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
题目大意 : 在1~10的7次方范围内给出n,要求出n!的位数。
 
解题思路 :由于数据很大 , 达到了10的7次方 ,如果先把n!结果算出来肯定会超时。
所以就想到,求一个数的位数,即可对这个数求对数 ,因为n!= 1*2*3*4*...n;所以lg(n!)=lg(1)+lg(2)+lg(3)+...lg(n);
(因为loga(b*c)=logab+logac,  a为底数,  并且lg(n)=log10(n);)
 
例如   当n!=99,(仅仅是比方),那么log10(99)算出来肯定比2要小一点,假设等于1.9几,那么我们要做的就是把这个值的小数部分去掉,然后直接加1,这里就得到2,而99就是两位,其他的数  ,一样的道理。
所以问题解决:
 
 
 1 #include<stdio.h>
 2 #include<math.h>
 3 int n,i,m;
 4 int main()
 5 {
 6     double sum;
 7     scanf("%d",&n);
 8     while(n--)
 9      {
10         sum=0;
11         scanf("%d",&m);
12         for(i=1;i<=m;i++)
13             sum+=log10(i);
14         printf("%d\n",(int)sum+1); //强制转换成int型 去掉sum值的小数
15      }
16      return 0;
17 }

 

posted @ 2013-08-22 14:11  Snail。  阅读(142)  评论(0编辑  收藏  举报