HDU 1060 Leftmost Digit (数学log)

题意:给定一个数n,让你求出n的n次方的第一位数。

析:一看这个n快到int极限了,很明显不能直接做,要转化一下。由于这是指数,我们可以把指数拿下来。

也就是取对数,设ans = n ^ n,两边取以10为底对数 lg(ans) = n * lg(10),然后这个整数部分都是10的多次方,

没什么用,也就是说我们要的小数部分,然后再取指数,就OK了。还要注意要用long long因为可能超int了,第一次忘了,WA了。

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long LL;

int main(){
    int n, T;  cin >> T;
    while(T--){
        cin >> n;
        double ans = n * log10(n);
        ans -= (LL)ans;//小数部分
        cout << (LL)pow(10, ans) << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5595913.html