加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数阶乘位数

发布时间:2020-12-14 03:16:45 所属栏目:大数据 来源:网络整理
导读:N! (N factorial) can be quite irritating and difficult to compute for large values of N. So instead of calculating N!,I want to know how many digits are in it. (Remember that N! = N * (N - 1) * (N - 2) * ... * 2 * 1) Input Each line of the

N! (N factorial) can be quite irritating and difficult to compute for large values of N. So instead of calculating N!,I want to know how many digits are in it. (Remember that N! = N * (N - 1) * (N - 2) * ... * 2 * 1)

Input

Each line of the input will have a single integer N on it 0 < N < 1000000 (1 million). Input is terminated by end of file.

Output

For each value of N,print out how many digits are in N!.

Sample Input
1
3 
32000 
Sample Output
1 
1 

130271

原理:利用迭代,原本计算公式应该是(n*(n-1)*...*2*1)/(10*10*...*10),计算能除多少个10,结果再加1. 现在是从第一个n开始就除10,判断能否除完以后的结果>1,如果大于1,就result++。

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stack>
#include<cmath>
using namespace std;
int main()  
{  
    int n ;  
    while(scanf("%d",&n)!=EOF)  
    {  
        int result=1;  
        int i;  
        double temp = 1;  
        for(i=n;i>0;i--)  
        {  
            temp*=i;  
            while((temp/10)>1)  
            {  
                result++;  
                temp=temp/10;  
            }  
        }  
        printf("%dn",result);  
    }  
    return 0;  
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读