Big Number
2 10 20
7 19
题意:给定一个大数,求出这个数的阶乘的位数
解题思路:做过 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;
}