HDU ACM Steps:Leftmost Digit
题目描述
Given a positive integer N, you should output the leftmost digit of N^N.
输入
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).
输出
For each test case, you should output the leftmost digit of N^N.
输入样例
2
3
4
输出样例
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.
思路
这道题只能用数学来解。
1.我们需要知道
2.
3.因为b是纯小数,所以
代码
#include<stdio.h>
#include<math.h>
typedef long long ll;
int t,n;
ll a;//注意不能用int,会溢出
double b,c;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
c=n*log10(n*1.0);
a=(ll)c;
b=c-a;
printf("%d\n",(ll)pow(10.0,b));
}
return 0;
}