HDOJ 1060 Leftmost Digit
生活随笔
收集整理的這篇文章主要介紹了
HDOJ 1060 Leftmost Digit
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Author Ignatius.L題目大意:1.第一行輸入一個整數(shù)T代表接下來有T組測試數(shù)據(jù)。2.接下來的T行,每行輸入一個整數(shù)(1<=N<=1,000,000,000)。3.輸出結(jié)果為N^N(N的N次方)最左邊的那一位數(shù)(即最高位)。4.注意:每行輸出一個結(jié)果。解題思路:1.令M = N^N2.兩邊取對數(shù),log10M = N*log10N,得到M = 10^(N*log10N)3.令N^(N*log10N) = a(整數(shù)部分) + b(小數(shù)部分),所以M = 10^(a+b) = 10^a *10^b,由于10的整數(shù)次冪的最高位必定是1,所以M的最高位只需考慮10^b4.最后對10^b取整,輸出取整的這個數(shù)就行了。(因?yàn)?<=b<1,所以1<=10^b<=10對其取整,那么的到的就是一個個位,也就是所求的數(shù))。需要注意的地方:關(guān)于取整:可以用強(qiáng)制類型轉(zhuǎn)換(int)10^b,也可以用floor函數(shù)floor(10^b), 但要注意的問題是floor函數(shù)是double型的,若用floor函數(shù),則在輸出時要用"%.0lf\n", (有關(guān)floor函數(shù)和ceil函數(shù),詳見http://baike.baidu.com/view/2873705.htm)
// hdoj_1060 Leftmost Digit // 0MS 236K 345 B GCC #include <stdio.h> #include <math.h> int main(void) {int n, i, ncase;long long x;scanf("%d", &ncase);for(i = 0; i < ncase; i ++){scanf("%ld", &n);double m = n * log10((double)n);double g = m - (long long)m;g = pow(10.0, g);printf("%d\n", (int)g);}return 0; }
/*求num的最左位上的數(shù):設(shè)num=a.~*10^n; a即為所求lg(num)=n+lg(a.~);->:lg(a.~)=lg(num)-n;又n為num的總位數(shù)減1,n=(int)lg(num);->:a.~=pow(10,1g(num)-(int)(lg(num))); */ #include <cstdio> #include <cmath> #include <iostream> #include <string> using namespace std;int main() {int cas;scanf("%d",&cas);while (cas--){double num;scanf("%lf",&num);double x=num*log10(num);x-=(__int64)x;int ans=pow(10.0,x);printf("%d\n",ans);}return 0; }
?
? ?
轉(zhuǎn)載于:https://www.cnblogs.com/Lee-geeker/p/3369112.html
總結(jié)
以上是生活随笔為你收集整理的HDOJ 1060 Leftmost Digit的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。