标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018
题意:求n!的数位(即n!有多少位);
思路:对于一个数x,它的数位ans=log10(x);
证明:假设pow(10, y-1) <= x < pow(10, y)-----1,显然有ans(x)=y;
由1式可得 y-1 <= log10(x) < y;
所以 ans(x) = (int) log10(x) + 1;
此题中代入 x = n! = 1*2*3....*n, 可得 ans(n!) = (int)log10(n!)+1 = (int)log10(1*2*3...*n)+1 = int ( log(10)1 + log10(2) + log10(3) + ... + log10(n) ) + 1;
代码:
1 #include <iostream>
2 #include <stdio.h>
3 #include <math.h>
4 #define ll long long
5 using namespace std;
6
7 int main(void){
8 int t;
9 while(~scanf("%d", &t)){
10 while(t--){
11 int n;
12 double cnt=0;
13 scanf("%d", &n);
14 for(int i=1; i<=n; i++){
15 cnt+=log10(i);
16 }
17 ll ans=cnt+1;
18 printf("%lld\n", ans);
19 }
20 }
21 return 0;
22 }
标签:
原文地址:http://www.cnblogs.com/geloutingyu/p/5978149.html