HDU ACM Steps:Leftmost Digit

HDU ACM Steps:Leftmost Digit

题目描述

Given a positive integer N, you should output the leftmost digit of N^N.

输入

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).

输出

For each test case, you should output the leftmost digit of N^N.

输入样例

2
3
4

输出样例

2
2
Hint
In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2.
In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2.

思路

这道题只能用数学来解。
1.我们需要知道 N N = 1 0 l o g 10 N N N^N=10^{log_{10}^{N^N}}
2. c = l o g 10 N N c = a + b a b N N = 1 0 a × 1 0 b . 令c=log_{10}^{N^N},c=a+b,a是整数,b是纯小数。所以N^{N}=10^a\times10^b.
3.因为b是纯小数,所以 1 < 1 0 b < 10 1 0 b 1<10^b<10,也就是说,10^b的整数部分就是我么所求的答案

代码

#include<stdio.h>
#include<math.h>
typedef long long ll;
int t,n;
ll a;//注意不能用int,会溢出
double b,c;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		c=n*log10(n*1.0);
		a=(ll)c;
		b=c-a;
		printf("%d\n",(ll)pow(10.0,b));
		
	}
	return 0;
 }
发布了13 篇原创文章 · 获赞 2 · 访问量 339

猜你喜欢

转载自blog.csdn.net/weixin_45718149/article/details/104631540