杭电ACM1060——Leftmost Digit

来源:互联网 发布:vb和微信公众号对接 编辑:程序博客网 时间:2024/06/05 13:31

这一题,主要是数学方面的知识,将ans = n^n进行化简。

两边取log。得到log(ans)= n * log(n);

再推出ans = 10 ^ (n * log (n));10的整数幂,只会在ans后面加上一个0,小数的幂才会影响ans的其他有效数字。

所以求出n * log(n)的小数部分temp,然后(int)10^temp就是所要求的结果。

数学,很重要!!~·

下面的是AC的代码,用C++提交,G++会CE:

#include <iostream>#include <cmath>using namespace std;int main(){int n, t;while(cin >> t){while(t--){cin >> n;double temp1 = n * log10(double(n));__int64 temp2 = __int64(temp1);double temp = temp1 - temp2;int a = (int)pow(10.0, temp);cout << a << endl;}}return 0;}


1 0