您的位置:首页 > 其它

hdu 1060——Leftmost Digit

2012-06-20 21:07 239 查看
[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.


[align=left]Author[/align]
Ignatius.L

水题,算是为数学题目开头吧

算法思想:

由于算的是 n^n 的第一位数,刚开始有点犯难,但是后来释然了, n^n = 10 ^ (log10(n^n)) = 10 ^ (n * log10(n))

然后我们可以观察到: n^n = 10 ^ (N + s) 其中,N 是一个整数 s 是一个小数。

好了,结果出来了

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
double m=n*log10(n);
m=m-(__int64)(m);
m=pow(10.0,m);
printf("%d\n",(int)(m));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: