
Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16762 Accepted Submission(s): 6643
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 的最高位是什么数字,第一行 t 代表测试数据组数
显然这不是模拟能解决的n有10亿,那么就肯定是数学题了
e = log10(N^N) = N * log10(N) (对数公式)
那么 10^e == N^N 然后想想 10^floor(e) 等于什么呢,不就是与 N^N 相同的位数,但最小的数吗?就是 1 后面都是 0 的数
而 floor( 10^(e-floor(e)) ) 就是就是要求的了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std; int main()
{
int t;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
double temp=n*log10(n*1.0);
double res=temp-floor(temp);
printf("%d\n",(int)pow(10.0,res));
}
return ;
}