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 }

 

posted @ 2018-09-01 13:15  王陸  阅读(565)  评论(0编辑  收藏  举报