哎,果然还是写一写解题报告,许多题目才不会忘记...
题目意思是输入一个数N,求N^N的最左边的一个数,题目看起来简单,于是想了想大数快速幂,果然还是存不下,而且又麻烦,最后看别人的分析才知道数学方便的问题
设M=N^N,则log10(M)=N*log10(N);
所以M=10^(N*log10(N));
10的整数次幂都为10的倍数,10的倍数首位都为1,其他位都为0,所以我们要考虑小数部分
即10^(N*log10(N))(取小数部分) 然后取整就行了
Problem Description
Given a positive integer N, you shouldoutput 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 thenumber of test cases. T test cases follow.
Each test case contains a single positive integerN(1<=N<=1,000,000,000).
Each test case contains a single positive integerN(1<=N<=1,000,000,000).
Output
For each test case, you should outputthe leftmost digit of N^N.
Sample Input
2 3 4
Sample Output
2 2题目意思是输入一个数N,求N^N的最左边的一个数,题目看起来简单,于是想了想大数快速幂,果然还是存不下,而且又麻烦,最后看别人的分析才知道数学方便的问题
设M=N^N,则log10(M)=N*log10(N);
所以M=10^(N*log10(N));
10的整数次幂都为10的倍数,10的倍数首位都为1,其他位都为0,所以我们要考虑小数部分
即10^(N*log10(N))(取小数部分) 然后取整就行了
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int i,j,m,r,n;
double k,x;
scanf("%d",&n);
while(n--)
{
scanf("%lf",&k);
x=k*log10(k)-(long long)(k*log10(k));
printf("%d\n",(int)pow(10,x));
}
}