hdu1018(数位)

题目链接: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;

代码:

 #include <iostream>
#include <stdio.h>
#include <math.h>
#define ll long long
using namespace std; int main(void){
int t;
while(~scanf("%d", &t)){
while(t--){
int n;
double cnt=;
scanf("%d", &n);
for(int i=; i<=n; i++){
cnt+=log10(i);
}
ll ans=cnt+;
printf("%lld\n", ans);
}
}
return ;
}
上一篇:bzoj 2428: [HAOI2006]均分数据 随机化


下一篇:[环境配置]Ubuntu16.04下编译安装gcc6.3.0