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;}