hdu1060 Leftmost Digit---求N的N次方的首位(对数)

题目链接:

 http://acm.hdu.edu.cn/showproblem.php?pid=1060

题目描述:
求N的N次方的第一位。

思路:

第一次做这种类型的题目,学到了如何运用对数。

首先推导下述公式

k的以10为底的对数值必定是A.B的形式,A为结果的整数部分,B为小数部分

这里求nn的首位只需要求出A.B = Nlog(N),然后取小数位即可,因为10A并没有什么用,只要算出100.B

就算出了首位数字。这种方法可以用来求前i位。(i数值要小,因为运算精度有限) 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 using namespace std;
 5 typedef long long ll;
 6 int T, n, m, cases;
 7 int main()
 8 {
 9     cin >> T;
10     while(T--)
11     {
12         cin >> n;
13         double s = 1.0 * n * log10(1.0 * n);
14         s -= (ll)s;
15         s = pow(10, s);
16         while(s < 1)s *= 10;
17         cout<<(int)(s)<<endl;
18     }
19     return 0;
20 }
原文地址:https://www.cnblogs.com/fzl194/p/8682653.html