HDU 1060

Leftmost Digit

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


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.
 1  //x ^ x= 10^(x*lg(x))=10^(整数部分+小数部分) ;则x ^ x的最高位是由小数部分决定的(因为10的整数次幂不会影响最高位,只在最末位加0)。
 2  //去掉整数部分(floor函数向下去整)得到10^(小数部分)最高位即是所求……
 3  #include <iostream>
 4  #include <cmath>
 5  using namespace std;
 6  
 7  int main()
 8  {
 9      int i,j,k,T;
10      double ans;
11      int num;
12      cin>>T;
13      while(T--)
14      {
15          cin>>num;
16          ans = log10((double)num);
17          ans=ans-(int)ans;
18          ans=ans*num;
19          ans=ans-(int)ans;
20          num=(int)pow(10.0,ans);//10的小于1的数次方肯定只有一位 
21          cout<<num<<endl;
22      }
23      return 0;
24  }
25  //注意:涉及到数学函数最好g++提交,否则CE 

 

posted @ 2012-09-04 22:06  加拿大小哥哥  阅读(512)  评论(0编辑  收藏  举报