HDU 1060 Left-most Digit

传送门

Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15305    Accepted Submission(s): 5937


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
 
Solution
取对数。
设 n^n = x*10^y(n^n的科学计数法表示),
则 log (n^n) = y + log x
 
Implementation
#include <bits/stdc++.h>
using namespace std;

int main(){
    int T;
    scanf("%d", &T);
    for(int n; T--; ){
        scanf("%d", &n);
        double t=log10(n)*n;
        printf("%d
",(int)pow(10, t-floor(t)));
    }
}

Tips

<cmath>内常用函数:

pow 

exp

log  自然对数

log10 常用对数

log2  以2为底的对数

floor

ceil

原文地址:https://www.cnblogs.com/Patt/p/5003819.html