您的位置:首页 > 其它

HDU--1060--Leftmost Digit

2017-07-05 09:45 513 查看
Description
Given a positiveinteger N, you should output the leftmost digit of N^N. 
Input
The inputcontains several test cases. The first line of the input is a single integer Twhich 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 testcase, you should output the leftmost digit of N^N. 
Sample Input
 2
3
4
Sample Output
 2
2

 题意:输出N^N的位数

 正常方法写肯定会超时,或者超出int的范围,所以这里要用到一种方法。

1.令M = N^N 

2.两边取对数,log10M = Nlog10N,得到M = 10^(Nlog10N) 

3.令N^(N*log10N) = a(整数部分) + b(小数部分),所以M = 10^(a+b) = 10^a *10^b,由于10的整数次幂的最高位必定是1,所以M的最高位只需考虑10^b 

4.最后对10^b取整,输出取整的这个数就行了。(因为0<=b<1,所以1<=10^b<=10对其取整,那么的到的就是一个个位,也就是所求的数)。

AC代码如下:

#include<stdio.h>
#include<math.h>
int main()
{
int n,a;
double b,c;
scanf("%d",&n);
while(n--)
{
scanf("%d",&a);
b=a*log10((double)a);
c=b-(long long)b;
printf("%d\n",(int)pow(10,c));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: