hdu 1060——Leftmost Digit

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.


 

Author
Ignatius.L

 

 

水题,算是为数学题目开头吧

算法思想:

由于算的是 n^n 的第一位数,刚开始有点犯难,但是后来释然了, n^n = 10 ^ (log10(n^n)) = 10 ^ (n * log10(n))

然后我们可以观察到: n^n = 10 ^ (N + s) 其中,N 是一个整数 s 是一个小数。

好了,结果出来了

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        double m=n*log10(n);
        m=m-(__int64)(m);
        m=pow(10.0,m);
        printf("%d\n",(int)(m));
    }
    return 0;
}


 

你可能感兴趣的:(算法,Integer,input,each,output)