HDU 1060 Leftmost Digit (數論)

來源:互聯網
上載者:User

Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13680    Accepted Submission(s): 5239


Problem Description Given a positive integer N, you should output the leftmost digit of N^N.
 
Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
 
Output For each test case, you should output the leftmost digit of N^N.
 
Sample Input

 2 3 4   

Sample Output
 2 2 Hint In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2. In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2. 






題目大意:給正整數n,問n^n的最左面的那位元是多少。


解析:高大上的數論,確實對數學的智商不怎麼夠用。下為借鑒網上大神的思路: 一個數是由每一位的基數乘以相對應的權值,例如 123456 , 基數"1"的權值為 10^5,  基數 "2" 的權值為 10^4......所以該題要求的就是最高位的基數。     對 x^x 取對數,得 x* ln( x )/ ln( 10 ), 現假設這個值為 X.abcdeefg   那麼 10^X 就是 最高位對應的權值,10^ 0.abcdefg 就是最高位的基數。注意這裡得到的並不是一個整數,為什麼呢。 因為這裡是強行將後面位的值也轉化到最高位上來了,這有點像大數中,如果不滿進位卻強行進位,顯然那樣會進給高位一個小數而不是一個天經地義的整數。得到 10^ 0.abcdefg 後,再用 double floor ( double ) 函數取下整就得到最高位的數值大小了


詳解請參見部落格: http://www.cnblogs.com/jackge/archive/2013/01/03/2842830.html





AC代碼:

#include <iostream>#include <cstdio>#include <cmath>using namespace std;int main(){//    freopen("in.txt", "r", stdin);    int t, n;    scanf("%d", &t);    while(t--){        scanf("%d", &n);        double foo = n * log10(double(n));       //需強轉        double ans = foo - floor(foo);        printf("%d\n", (int)pow(10.0, ans));    }    return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.