bit 1046 Leftmost Digit

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值