您的位置:首页 > 其它

HDU 1060

2012-09-04 22:06 183 查看

Leftmost Digit

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


[align=left]Problem Description[/align]
Given a positive integer N, you should output the leftmost digit of N^N.

[align=left]Input[/align]
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).

[align=left]Output[/align]
For each test case, you should output the leftmost digit of N^N.

[align=left]Sample Input[/align]

2 3 4

[align=left]Sample Output[/align]

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.

//x ^ x= 10^(x*lg(x))=10^(整数部分+小数部分) ;则x ^ x的最高位是由小数部分决定的(因为10的整数次幂不会影响最高位,只在最末位加0)。
//去掉整数部分(floor函数向下去整)得到10^(小数部分)最高位即是所求……
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int i,j,k,T;
double ans;
int num;
cin>>T;
while(T--)
{
cin>>num;
ans = log10((double)num);
ans=ans-(int)ans;
ans=ans*num;
ans=ans-(int)ans;
num=(int)pow(10.0,ans);//10的小于1的数次方肯定只有一位
cout<<num<<endl;
}
return 0;
}
//注意:涉及到数学函数最好g++提交,否则CE
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: