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
这题是不能直接算的,无论时间还是空间都吃不消。此题用取对数的方法。例如:1234567取对数后为log10(1.234567*10^6)=6+log10(1.234567)(log10(1.234567)为小数部分),而10^(log10(1.234567))为1.234567,取整就得到了我们想要的结果。
代码如下:
#include<stdio.h>
#include<math.h>
int main()
{
    int t;
    double ans,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf",&n);
        ans=log10(n);//这里我没用n*log10(n)是因为,后面取整的时候,
// n*log10(n)会超出int的范围,当然也可以用__int64来取整。
ans
-=int(ans); ans*=n; ans=ans-int(ans); ans=pow(10,ans); printf("%d ",int(ans)); } return 0; }
原文地址:https://www.cnblogs.com/duan-to-success/p/3486701.html