数学——Leftmost Digit

Leftmost Digit

时间限制: 1000ms
内存限制: 32768KB
HDU       ID:  1060
64位整型:      Java 类名:

题目描述

Given a positive integer N, you should output the leftmost digit of N^N.

输入

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).

输出

For each test case, you should output the leftmost digit of N^N.

样例输入

2
3
4

样例输出

2
2

提示

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.

m=n^n;两边同取对数,得到,log10(m)=n*log10(n);再得到,m=10^(n*log10(n));

然后,对于10的整数次幂,第一位是1,所以,第一位数取决于n*log10(n)的小数部分


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

floor函数 即上取整数    eg:x3.14,floor(x)=3




你可能感兴趣的:(数学,HDU)