题目链接:
http://www.51nod.com/Challenge/Problem.html#problemId=1058
1058 N的阶乘的长度
输入N求N的阶乘的10进制表示的长度。例如6! = 720,长度为3。
收起
输入
输入N(1 <= N <= 10^6)输出
输出N的阶乘的长度输入样例
6输出样例
3
思路:
n!对10取对数,这个数向下取整加一就是位数,对数运算有 lg(m*n)=lg(m)+lg(n),因此计算lg(n!)可以按照如下程序进行计算
This is the code:
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
int read()//输入外挂
{
int ret=0, flag=0;
char ch;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
ret = ch - '0';
while((ch=getchar())>='0'&&ch<='9')
ret=ret*10+(ch-'0');
return flag ? -ret : ret;
}
int main()
{
int n;
scanf("%d",&n);
double sum=0;
for(int i=n;i>=1;i--)
sum+=log10(i*1.0);
printf("%d\n",(int)sum+1);
return 0;
}