Leftmost digit time limit: 1 second memory limit: 1024kb
Description
Given a positive integer N, please write a program to get the leftmost digit of N !. (1 <= n <= 10 ^ 7 ).
Input
The input contains several test cases, the first line of the input is an integer T (t <= 1000), indicates the number of test cases.
The next n lines each has an positive N (1 <= n <= 10 ^ 7 ).
Output
For each test case, please write a line which is the leftmost digit of N !.
Sample Input
3
1
5
10
Sample output
1
1
3
Thanks to the idea provided by the great gods: converting a factorial into a logarithm addition operation solves the problem of high precision. And the sterling Formula
.
Reference: zhangwei1120112119 column bit1046 leftmost Digit
# Include <stdio. h> # include <math. h> # define PI ACOs (-1.0) # define e exp (1.0) int main () {int testcases; int N; double result_log; int I; scanf ("% d", & testcases); While (testcases --) {scanf ("% d", & N); result_log = 0; If (n> 500) {result_log = log10 (2 * n * PI)/2.0; result_log + = N * (log10 (N)-log10 (e);} else {++ N; for (I = 2; I <n; ++ I) {result_log + = log10 (I) ;}} result_log-= floor (result_log ); printf ("% d \ n", INT (POW (10.0, result_log);} return 0 ;}