杭电HDOJ 1060 解题思路
2013-05-03 15:32
169 查看
Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10180 Accepted Submission(s): 3867
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).
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.
Author
Ignatius.L
解题思路:
对于M^N的最左侧数的基本方法就是取对数。
M^N=10*N*log10(M);
log10(M)分成两部分:整数部分s(10^s=100……00最左侧永远是1)、小数部分t;
取小数部分t,t*N=a+b,整数部分a(10^a=100……00最左侧永远是1)、小数部分b;
取小数部分b,pow(10,b)取左侧第一位。
代码:
#include <stdio.h> #include <math.h> int main(int argc, const char * argv[]) { int n,a,l; double t; scanf("%d",&n); while(n--) { scanf("%d",&a); t=log10(a); l=t/1; t-=l; t*=a; l=t/1; t-=l; t=pow(10,t); l=t/1; while(l>9) l/=10; printf("%d\n",l); } }
相关文章推荐
- 杭电 2553 N皇后问题 递归回溯 打表 附解题思路
- 杭电 2037 今年暑假不AC 贪心算法 附解题思路
- [hdoj解题报告][hdoj_1060]Leftmost Digit 左值
- 杭电HDOJ 1002 解题报告
- 杭电DP题库及解题思路
- 杭电HDOJ 1003 解题报告
- 杭电HDOJ 1042 解题报告
- 杭电HDOJ 1160 解题报告
- 杭电2154解题思路
- 杭电2056 解题思路
- 杭电HDOJ 2084 解题报告
- 杭电HDOJ 1061 解题报告
- 杭电 1969 Pie 二分法 附翻译 解题思路
- 杭电HDOJ 1159 解题报告
- 杭电HDOJ 1097 解题报告
- HDOJ_2013蟠桃记(数学归纳) 解题思路总结
- 杭电HDOJ 1000 解题报告
- 杭电HDOJ 1001 解题报告
- 杭电OJ题 1678 Shopaholic 解题报告
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法