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

题目链接

n n = x n^n=x nn=x

log 10 n n = n × l o g 10 n = l o g 10 x \therefore \log_{10}{n^n}=n\times log_{10}{n}=log_{10}{x} log10nn=n×log10n=log10x

x = 1 0 n × l o g 10 n \therefore x=10^{n \times log_{10}{n}} x=10n×log10n

n × l o g 10 n = a . b n \times log_{10}{n}=a.b n×log10n=a.b,其中a确定了x的位数,b确定了每一位的值。

所以利用 1 0 b 10^{b} 10b取整就可以求出第一位数的值。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
	Finish_read = 0;
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') {
			f = -1;
		}
		if (ch == EOF) {
			return;
		}
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	x *= f;
	Finish_read = 1;
};

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int t;
	read(t);
	for (ll Case = 1, n; Case <= t; ++Case) {
		read(n);
		double ans = n * log10(n * 1.0);
		ans -= ll(ans);
		ans = pow(10, ans);
		printf("%lld\n", ll(ans));
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}