Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15967 Accepted Submission(s): 6242
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.
Author Ignatius.L Recommend We have carefully selected several similar problems for you: 1018 1061 1071 1573 1066
题解: 计算出N^N最左边的数,就是最高位的数。 设N^N=d.xxxxx * 10^(k-1),其中k表示N^N的位数,那么d.xxxxx=10^lg(N^N-(k+1)),再对d.xxxx取整即可获得最终结果。因为k等于lgN^N的整数部分加一,即k=lgN^N+1(取整),所以d=10^(lgN^N-lg10N^N)(取整)。
AC代码:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t,N;
double x=0.0;
cin>>t;
while(t--){
cin>>N;
x=N*log10((double)N);
x-=(long long)x;
x=(int)pow(10,x);
cout<<x<<endl;
}
return 0;
}