天天看點

HDUOJ 1060 Leftmost Digit(求最左位公式)

              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;
}