hdu 1060 Leftmost Digit

Leftmost Digit

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


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^n的结果的个位数
 
题解:将n^n用科学计数法表示为n^n=a*10^x (如274=2.74*10^2)将等式两边都取对数的lg n^n=lg(a*10^x)  
n*lgn=lga+x*lg(10)   ==    n*lgn=lga+x  所以lga=n*lgn-x 由指对互换得a=10^(n*lgn-x)    x为n^n的位数减一,如1234  ==n^n则x=3   同样有指对互化得lg(n^n)=x可化为10^x=n^n,所以x为 lg(n^n)向下取整
#include<stdio.h>
#include<string.h>
#include<cstdio> 
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 30000
#define mod 100
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
int main()
{
	LL n,m,j,i,t,k;
	scanf("%lld",&t);
	DD sum;
	while(t--)
	{
		scanf("%lld",&n);
		sum=(long DD)n*log10(n);
		sum-=(LL)sum;
		k=pow(10,sum);
		printf("%lld\n",k);
	}
	return 0;
} 

  

posted @ 2016-03-24 16:19  非我非非我  阅读(137)  评论(0编辑  收藏  举报