天天看點

Leftmost Digit(數學)

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 }      

轉載于:https://www.cnblogs.com/wkfvawl/p/9570013.html