Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2694 Accepted Submission(s): 1165
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).
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 2HintIn 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
运用log函数,由题得M=N^N,两边取对数得
运用log函数,由题得M=N^N,两边取对数得
lgM=N*lg(N)---->M=10^N*lg(N);
在N*lg(N)中,有小数部分,以及整数部分,10的整数部分次幂,为100......0,所以最高位取决于其小数部分。
最后求pow(10.0,小数部分)
在N*lg(N)中,有小数部分,以及整数部分,10的整数部分次幂,为100......0,所以最高位取决于其小数部分。
最后求pow(10.0,小数部分)
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int T;
int ans;
cin>>T;
while(T--)
{
ans=0;
unsigned long a;
cin>>a;
//运用对数,求解最高位数
double dyx=a*log10(a*1.0);
//求解下来,dyx为其小数部分。
dyx-=(__int64)dyx;
ans=(int)pow(10,dyx);
cout<<ans<<endl;
}
return 0;
}