HDUOJ1060Leftmost Digit

Leftmost Digit

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

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=p= x*10^m  所以log(p)=n*log10(n);

而这和我们要求的最高位i的那个数有什么关系乐 比如求1234的最高位为1,而log10(1234)=log10(1.234*10^3)=log10(1.234)+3;

而最高位即为其pow(10,1og10(1.234))=1.234的整数1;

=log10(n^n)=n*log10(n);所以同样的道理,就可以求这个了.....

代码:

 1 #include<stdio.h>
 2 #include<math.h>
 3 int main()
 4 {
 5     int n,m;
 6     scanf("%d",&m);
 7     while(m--)
 8     {
 9         scanf("%d",&n);
10         double a=n*log10(1.0*n);
11         a-=(__int64)a;
12         printf("%d\n",(int)pow(10,a));
13     }
14     return 0;
15 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区

领取腾讯云代金券