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).
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 2HintIn 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
题意:
求n^n阶乘的最左边的一位;
分析:
考虑到本题的数据太大,故不可直接计算。
选择用log函数,
有m=n^n;两边取对数,得log10(m)=nlog10(n);得m=10^(nlog10(n));
因为10的阶乘得到数首位是1;所以只许看nlog10(n)的小数部分;
10^0=1<10^x(0<x<1)<10^1=10;
所以只需判断nlog10(n);
代码:
#include<stdio.h>
#include<math.h>
int main(){
int n,i,j,m;
__int64 b,d;
double a,c;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
a=m*log10(m);
b=(__int64)a;
c=a-b;
d=(__int64)(pow(10,c));//pow函数模型pow(float a,float b)
printf("%I64d\n",d);
}
return 0;
}