HDU 1060 Leftmost Digit(求 n ^ n 的最高位的值)



Leftmost Digit
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u


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 的最高位的數值是多少。

對一個數num可寫爲 num=10n + a, 即科學計數法,使a的整數部分即爲num的最高位數字
numnum=10n + a 這裏的n與上面的n不等
兩邊取對數: num*lg(num) = n + lg(a);
因爲a<10,所以0<lg(a)<1
令x=n+lg(a); 則n爲x的整數部分,lg(a)爲x的小數部分
又x=num*lg(num);
a=10(x-n) = 10(x-int(x)))
再取a的整數部分即得num的最高位

附上代碼:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		double n;
		scanf("%lfd",&n);
		double a = n * log10(n);
		long long int b = (long long int)a;
		double c = a - b;
		long long int ans = (long long int)pow(10,c);
		printf("%I64d\n",ans);
	}
	return 0;
}


發佈了106 篇原創文章 · 獲贊 31 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章