Big Number

来源:互联网 发布:java使用redis统计 编辑:程序博客网 时间:2024/06/04 18:04

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


题意:给定一个大数,求出这个数的阶乘的位数


解题思路:做过 nyoj 28 大数阶乘 的很容易会想到用同样的方法做这个题,但是提交往往会发现超时,因为这个题给出的测试数据明显比那个题大的多,所以此处就要改变方法了;以前在数学中我们学过,对于任意一个数n求其位数的公式为  len=(int)log10(n)+1,那么len就是这个数的位数,同样的道理,求一个数的阶乘的位数,仍然用此种方法:

例如:求10!的位数len

则len=(int)log10(10!)+1

又len=(int)log10(1*2*3*....*9*10)+1

既len=(int){log10(1)+log10(2)+......+log10(9)+log10(10)}+1

到次处,已经把10!转换成求10个数的位数的子式,利用log10(a)这个函数分别求出相应的位数即可


#include<stdio.h>
#include<math.h>


int main(int argc,char * argv[]){

int n=0;

scanf("%d",&n);

while(n--){

int m=0;
scanf("%d",&m);

double len=0;

for(int i=1;i<=m;i++)
len+=log10(i);//求阶乘的位数:len= (int)log10(n)+1; 

printf("%d\n",(int)len+1);

}


return 0;















 


0 0