HDU 1018 mathematics

Hang wondered n! How many digits as n relatively large, this number is difficult to express as a mathematical comparison Hang vegetables, so please help this talented ACMer.

To make this question become an attendance problem, Hang give you a formula

image.png

Stirling formula is used to find the n! Approximation, when larger n value when the more accurate

Hang also good to tell you e = 2.718281828

Even gave a formula, I have not seen such a good person out of question

Input
The first line one integer t, t group, I forgot how t

One for each integer n (1 <= n <= 1e7)

The Output
T lines, each an integer representing the n! Bits

SampleInput
2
10
20
SampleOutput
7
19
meaning of the questions: seek n! Bits
ideas: directly sets the value calculated by the formula, and then sets loglO () function can be. This problem O (n) n terms is accumulated before the okk.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int t,n;

    cin >> t;

    while(t--)
    {
        double s=0;

        cin >> n;

        for(int i=1;i<=n;i++)
            s+=log10(i);///相加在log里转换为相乘就变成了阶乘

        cout << (int)s+1 << endl;
    }

    return 0;
}
Published 54 original articles · won praise 0 · Views 1236

Guess you like

Origin blog.csdn.net/weixin_44144278/article/details/98804957