您的位置:首页 > 其它

【杭电-oj】 -1060-Leftmost Digit(输出n的n次方最左边数)

2016-04-17 15:21 459 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: