Leftmost Digit

来源:互联网 发布:javascript json类型 编辑:程序博客网 时间:2024/06/05 12:47

 

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
234
Sample Output
22
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.

1、题意:给你一个数,然后输出n的n次方的结果的首位数字。
2、思路:一看这个数就是非常大,用数组来算大数也是不行的,所以要用特殊的方法,然后从网上别的大佬那里学到了方法。

设M=N^N,则log10(M)=N*log10(N);
所以M=10^(N*log10(N));
10的整数次幂都为10的倍数,10的倍数首位都为1,其他位都为0,再考虑小数部分
即10^(N*log10(N))(取小数部分) 然后取整。

3、代码:


#include<stdio.h>
#include<math.h>
int main()
{
 int t;
 double n,k,a;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%lf",&n);
  a=n*log10(n);
  k=a-(long long)a;
  printf("%d\n",int(pow(10,k)));
 }
 return 0;
}

4、总结:又学到了一些方法。