Leftmost Digit(hdu1060)(数学题)

Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16762    Accepted Submission(s): 6643


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 的最高位是什么数字,第一行 t 代表测试数据组数
显然这不是模拟能解决的n有10亿,那么就肯定是数学题了
   e = log10(N^N) = N * log10(N) (对数公式)
那么 10^e == N^N 然后想想 10^floor(e) 等于什么呢,不就是与 N^N 相同的位数,但最小的数吗?就是 1 后面都是 0 的数
而 floor( 10^(e-floor(e)) ) 就是就是要求的了
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int t;
10     int n;
11     scanf("%d",&t);
12     while(t--)
13     {
14         scanf("%d",&n);
15         double temp=n*log10(n*1.0);
16         double res=temp-floor(temp);
17         printf("%d\n",(int)pow(10.0,res));
18     }
19     return 0;
20 }
View Code

 

 
 
posted @ 2016-10-15 17:20  happy_codes  阅读(141)  评论(0编辑  收藏  举报