码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1018 共同交流~

时间:2017-09-17 21:02:16      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:while   rmi   stdio.h   数值   span   include   others   clu   contains   

                                         Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38886    Accepted Submission(s): 18889


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
 
 
刚看到这道题,其实就应该知道,不会是简单的用一个阶乘函数来解答,因为它的数值太大了;
 
所以,我们可以换个思路:

//求一个超大数阶乘的位数
//所谓n!的十进制位数,就是 log(n)+1, 根据数学公式有:n!=1*2*3*.....*n;
//lg(n!)=lg(2)+......lg(n);
//则位数 = log(n)+1;

 
代码如下:

#include<stdio.h>
#include<math.h>
double f(int n)
{
  double cnt=0;
  for(double i=2;i<=n;i++) // i也要为double型,否则会出现歧义 !
  {
    cnt += log10(i);
  }
  return cnt;
}

int main()
{
  int cas,n;
  scanf("%d",&cas);
  while(cas--)
  {
    scanf("%d",&n);
    printf("%d\n",(int)f(n) + 1);
  }
  return 0;
}

hdu 1018 共同交流~

标签:while   rmi   stdio.h   数值   span   include   others   clu   contains   

原文地址:http://www.cnblogs.com/shirley-0021/p/shirley-21.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!