1. 程式人生 > >【杭電-oj】 -1060-Leftmost Digit(輸出n的n次方最左邊數)

【杭電-oj】 -1060-Leftmost Digit(輸出n的n次方最左邊數)

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.

n^n=a*10^m;

兩邊對10去對數,得n*lg(n)=m+lg(a);

此處接下來用一個數學的pow,例如pow(n,m)表示n^m;

因為1<a<10,所以0<lg(a)<1.

接下來還有一個數學知識,即x^log(x)(n)=n;

#include<stdio.h>
#include<math.h>
int main()
{
	int t,ans;
	double n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf",&n);
		double x,a;
		x=n*log10(n);				
		a=pow(10,x-(__int64)x);			//x-(__int64)x,表示n*lgn的小數部分,即lg(a) 
		ans=int(a);
		printf("%d\n",ans);
	}
	return 0;
}