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表示为科学计数法:

N^N=a*10^m;

N*log10(N)=log10(a)+m;

a=10^(N*log(N)-m);

同时m即为N^N的位数,如log10(1000)=3,log10(N^N)取整即为m,由此推导出a=10^(N*log(N)-floor(N*log(N)),再对a取整即为所求答案

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long N;
        long double a,m;
        scanf("%I64d",&N);
        m=N*log10(N*1.0);
        a=pow((long double)10,m-floor(m));
        printf("%d\n",(int)a);
    }
}