hdu1018(阶乘位数 取对数||斯特林公式)

Big Number
Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.

Input Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 on each line.

Output The output contains the number of digits in the factorial of the integers appearing in the input.

Sample Input2
10
20
Sample Output7
19
Source Asia 2002, Dhaka (Bengal)
方法一:求1+(int)log10(n!)=1+log10(1)+log10(2)+……+log10(n)
注意遍历过程中是double,到最后再取整。

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
   
    long long n,a,i;
    double m;
    scanf("%lld",&n);
    while(n--)
    {
   
        scanf("%lld",&a);
        m=0;
        for(i=2;i<=a;i++)
            m+=log10(i);
        cout<<(int)m+1<<'\n';
    }
    return 0;
}

方法二:斯特林近似值公式:
求阶乘有多少位,即log10(n!)

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
const double pi=acos(-1.0);
int main()
{
   
    long long n,a,i;
    double m;
    scanf("%lld",&n);
    while(n--)
    {
   
        scanf("%lld",&a);
        m=0.5*log10(2*pi*a)+a*log10(a/exp(1));
        cout<<(int)m+1<<'\n';
    }
    return 0;
}

全部评论

相关推荐

影04714:把图书管理系统那个项目经验内容适当的减少掉,然后改成据为己有不要说团队项目,因为图书管理系统这类常见的谁来了都能独立写出来,提问能圆过来即可
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务