HDU 1060:Leftmost Digit

Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19366    Accepted Submission(s): 7677


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

Input
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).
 

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

Sample Input
2
3
4
 

Sample Output
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.

设N^N=x ,两边取对数:log(N*N)=N*log N=log x,10^(N*log N)=x,因为10的任何整数次方的首位都是1,所以x的首位数和N*log N的小数部分有关

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
int main()
{
	int t;
	ll n;
	double x;
	cin>>t;
	while(t--)
	{
		cin>>n;
		x=n*log10((double)n);
		x-=(ll)x;
		x=(int)pow(10,x);
		cout<<x<<endl;
	}
	return 0;
}


原文地址:https://www.cnblogs.com/Friends-A/p/9309021.html