1. 程式人生 > >Leftmost Digit(數學)

Leftmost Digit(數學)

ron number git int contain bsp for inpu namespace

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^n = 10 ^ (log10(n^n)) = 10 ^ (n * log10(n)),
然後我們可以觀察到: n^n = 10 ^ (N + s) 其中,N 是一個整數 s 是一個小數。由於10的任何整數次冪首位一定為1,所以首位只和s(小數部分)有關。

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #define ll long long int
 5 using namespace
std; 6 int main() 7 { 8 int t,n; 9 double m; 10 scanf("%d",&t); 11 while(t--) 12 { 13 scanf("%d",&n); 14 m=n*log10(n); 15 m=m-(ll)(m); 16 m=pow(10.0,m); 17 printf("%d\n",(int)(m)); 18 } 19 return 0; 20 }

Leftmost Digit(數學)