杭电 Big Number

来源:互联网 发布:mysql安装包下载 64位 编辑:程序博客网 时间:2024/04/28 03:43

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
21020
 

Sample Output
719
 

Source
Asia 2002, Dhaka (Bengal)
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1017 1016 1071 1019 1060 
题意:本题是要求阶乘的位数;
分析:此题看上去毫无头绪,但要是掌握了一个公式,就迎刃而解了;
x的位数就是(int)log10(x)+1;
推理如下:
对于任意一个给定的正整数a,
  假设10^(x-1)<=a<10^x,那么显然a的位数为x位,
  又因为
  log10(10^(x-1))<=log10(a)<(log10(10^x))
  即x-1<=log10(a)<x
  则(int)log10(a)=x-1,
  即(int)log10(a)+1=x
  即a的位数是(int)log10(a)+1
于是阶乘的位数便出来了,由于阶乘数比较大,故根据数学性质可写成循环实现;
代码如下:
 
int main(){int n,a,i,j;double s;scanf("%d",&n);for(i=0;i<n;i++){s=0;scanf("%d",&a);for(j=1;j<=a;j++)s+=log10(j);              //公式:x的位数就是(int)log10(x)+1;            printf("%d\n",(int)s+1); //故x阶乘的位数就是(int)log10(x!)+1;                }                         //因阶乘的位数较大,故x!用for循环来实现 return 0;}

0 0
原创粉丝点击