Leftmost Digit

时间:2023-03-09 05:23:34
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.

题意:给你一个数n让你求n^n最高位的数;

解题思路:那天上午在实验室写了这个题,刚开始看到数很大就寻思找规律,结果输出不到20位,数就太大了,回来之后想了好几天,后来又借鉴了大神的思路,知道了。

num^num=10^n*a;a的整数部分就是这个数的最左边的那一位; 对两边取对数  num*log10(num)= n+lg(a);n+lg(a)这个数中,n肯定是个整数,lg(a)肯定是个小数;

代码:



#include

using namespace std;

int main()

{

    //freopen("in.txt","r",stdin);

    unsigned long n;

    int t;

    scanf("%d",&t);

    while(t--)

    {

        scanf("%ld",&n);

        double x=n*log10(n*1.0);

        //cout<<"(__int64)x="<<(__int64)x<<endl;

        x-=(__int64)x;

        //cout<<"x="<<x<<endl;

        int a=pow(10.0, x);

        printf("%d\n",a);

    }

    return 0;

}