您的位置:首页 > 其它

HDU 1060 Leftmost Digit

2014-04-14 13:55 363 查看


Leftmost Digit

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

Total Submission(s): 12337 Accepted Submission(s): 4707



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.




本题很水,取个10的对数后整数部分可以用来确定结果的位数信息,但这里不需要位数,只要求最左边的数,整数部分与最左边的数没有任何关系,所以只需看小数部分即可,对小数部分求10的幂指下取整。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>

int main(){
	double res;
	int t,n,d;

	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		res = n*log10(n*1.0);
		res = res - floor(res);
		d = (int)floor(pow(10,res));
		printf("%d\n",d);
	}
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: