Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


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^N的最高位数字是多少

解题思路:一开始,拿到这道题的时候,我们应该想想,不可能让你死算求出N^N,然后再看最高位,所以,我们要想想如何将该问题进行转化,使其变得简单,不妨,我们来推导推导公式。



k的以10为底的对数值必定是A.B的形式,A为结果的整数部分,B为小数部分



所以我们要想求N^N的最高位,只需先除去

然后,结果的整数部分便是我们要求的N^N的最高位

其中要注意的一点是


这样有助于我们中间避免直接计算N^N

#include<stdio.h>
#include<math.h>
int main()
{
    int t,i,d,n;
    double a;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&n);
        a=n*log10(1.0*n);
        a=a-(__int64)a;
        d=(int)(pow(10.0,a));
        printf("%d\n",d);
    }
    return 0;
}
菜鸟成长记


Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐