您的位置:首页 > 其它

杭电HDOJ 1060 解题思路

2013-05-03 15:32 169 查看


Leftmost Digit

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

Total Submission(s): 10180 Accepted Submission(s): 3867



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

解题思路:

对于M^N的最左侧数的基本方法就是取对数。

M^N=10*N*log10(M);

log10(M)分成两部分:整数部分s(10^s=100……00最左侧永远是1)、小数部分t;

取小数部分t,t*N=a+b,整数部分a(10^a=100……00最左侧永远是1)、小数部分b;

取小数部分b,pow(10,b)取左侧第一位。

代码:

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
int n,a,l;
double t;

scanf("%d",&n);
while(n--)
{
scanf("%d",&a);
t=log10(a);
l=t/1;
t-=l;
t*=a;
l=t/1;
t-=l;
t=pow(10,t);
l=t/1;
while(l>9) l/=10;
printf("%d\n",l);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: