avatar
fireworks99
keep hungry keep foolish

HDU 1018 Big Number(求一个数的阶乘是几位数)

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 Input

2

10

20

Sample Output

7

19

题意

求一个数阶乘的位数

思路

456 = 4.56 * 10 ^ 2 三位数

4567 = 4.567 * 10 ^ 3 四位数

45678 = 4.5678 * 10 ^ 4 五位数

结论:问一个数有几位,将这个数向下取到10的N次方(456 -> 100, 4567 -> 1000) ,N + 1就是答案

总结

一个数的位数等于他对10取对数+1

log与log10

log: 求log e x,In x。返回计算结果。 double log(double x);

log10: 求log10x。返回计算结果。 double log10(double x);

Code

#include <cmath>
#include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        while(n--)
        {
            int tem;
            cin >> tem;
            double sum = 0;
            for(int i = 1; i <= tem; ++i)
                sum += log10(double(i));
            cout << (int)sum + 1 << '\n';
        }
    }
    return 0;
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy