Leftmost Digit
时间限制: 1秒 内存限制: 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
。
参考的大神blog:zhangwei1120112119的专栏 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;
}