杭电 Big Number


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 ≤ 10 7 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
 

Source
 

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值