HDOJ 1060 Leftmost Digit
生活随笔
收集整理的這篇文章主要介紹了
HDOJ 1060 Leftmost Digit
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Author Ignatius.L題目大意:1.第一行輸入一個整數T代表接下來有T組測試數據。2.接下來的T行,每行輸入一個整數(1<=N<=1,000,000,000)。3.輸出結果為N^N(N的N次方)最左邊的那一位數(即最高位)。4.注意:每行輸出一個結果。解題思路:1.令M = N^N2.兩邊取對數,log10M = N*log10N,得到M = 10^(N*log10N)3.令N^(N*log10N) = a(整數部分) + b(小數部分),所以M = 10^(a+b) = 10^a *10^b,由于10的整數次冪的最高位必定是1,所以M的最高位只需考慮10^b4.最后對10^b取整,輸出取整的這個數就行了。(因為0<=b<1,所以1<=10^b<=10對其取整,那么的到的就是一個個位,也就是所求的數)。需要注意的地方:關于取整:可以用強制類型轉換(int)10^b,也可以用floor函數floor(10^b), 但要注意的問題是floor函數是double型的,若用floor函數,則在輸出時要用"%.0lf\n", (有關floor函數和ceil函數,詳見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的最左位上的數:設num=a.~*10^n; a即為所求lg(num)=n+lg(a.~);->:lg(a.~)=lg(num)-n;又n為num的總位數減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; }
?
? ?
轉載于:https://www.cnblogs.com/Lee-geeker/p/3369112.html
總結
以上是生活随笔為你收集整理的HDOJ 1060 Leftmost Digit的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 幸运粒子换哪个宠物
- 下一篇: Deep Learning 学习随记(