【杭电oj】1060 - Leftmost Digit(数学好题)

Leftmost Digit

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


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


巧用lg转化,下面是我用mathtype打出的解题过程:

【杭电oj】1060 - Leftmost Digit(数学好题)_第1张图片


另外注意,对x取整时应该用__int64,用int会越界,用long int时oj会判PE。

代码如下:

#include <stdio.h>
#include <math.h>
int main()
{
	int u;
	int ans;
	double num,a;
	double x;
	scanf("%d",&u);
	while(u--)
	{
		scanf("%lf",&num);
		x=num*log10(num);
		a=pow(10.0,x-(__int64)x);
		ans=int(a);
		printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(【杭电oj】1060 - Leftmost Digit(数学好题))