http://acm.hdu.edu.cn/showproblem.php?pid=1060
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 casesfollow.
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
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.
代码
#include<stdio.h> #include<math.h> int main() { int n; double x,y; scanf("%d",&n); while(n--){ scanf("%lf",&x); y=x*log10(x); y-=__int64(y); y=pow(10.0,y); printf("%I64d\n",__int64(y)); } return 0; }
分析
输入x,求x^x的最高位
因为存在整数y>0使得x^x=10^y;
又有y=y整数部分+y小数部分;
所以x^x=10^y整数部分*10^y小数部分;
因为“10^y整数部分”只有进位效果,对最高位的值没影响;
而1=<10^y小数部分<10,从而“10^y小数部分”的整数部分就是所求的;
应指出的是代码中两处用到了__int64而不用int是应为double型转int会溢出导致出错。
0 条评论。