Big Number
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26865 | Accepted: 8581 |
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 <= m <= 10^7 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
java解法:
/** * 解释: 3、2、1、0的阶乘都是小于10的数,也就是1位数,所以,可以单独列出来,不多说; 自此,大于3的正整数,通过阶乘公式 n!= 1*2*3*...*(n-1)*n 求得,但是,此题的 目的并不在于此,而是求n的阶乘的位数。 而斯特林公式就是最好的选择,下面是百度百科的公式链接地址,不多说; http://baike.baidu.com/link?url=G6h67Aybh9i_jGt-GcLQcM2OourEQFcN_nhj3QmvvkYdHfIO4WLZlGgXR3Zx1gUKF-J7nkEFt-jfEOHV36T_aa 求阶乘结果的位数可用由 10^(x-1)≤n!≤10^x,再取对数获得(x表示位数,高中数学log10求解)。 */ import java.util.*; public class Big_Number{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while (n --> 0) { int n1 = sc.nextInt(); if (n1 <= 3) System.out.println(1); else { double result = 0.5 * Math.log10(2 * n1 * Math.PI) + n1 * Math.log10(n1 / Math.E) + 1; System.out.println((int) (result)); } } } }