HDU 1060 Leftmost Digit

来源:互联网 发布:海关数据开发客户 编辑:程序博客网 时间:2024/06/08 17:29

Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15759    Accepted Submission(s): 6137


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.
 

这道题运用的是数学方法。
假设S=n^n。两边同时取对数,得到lgS=nlgn。即有S=10^(nlgn)。
把nlgn看做一个整体,假设它是由整数加上介于0到1之间的小数相加得到的。
那么整数部分就不考虑了,就单纯的放大倍数而已。取决于小数部分。
小数部分=nlgn-(__int64)nlgn。注意是__int64。因为小数部分在0到1之间,所以10得次方得到的数必定大于等于1且小于10。所以再对得到的数取整即可。即(int)pow(10,小数部分)。
源代码:
<span style="font-size:18px;">#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string>#include<string.h>#include<math.h>#include<map>#include<vector>#include<algorithm>using namespace std;#define MAX 0x3f3f3f3f#define MIN -0x3f3f3f3f#define N 1005int main(){int T;__int64 num;double temp1;double temp2;scanf("%d", &T);while (T--){scanf("%lld", &num);temp1 = num*1.0*log10(num*1.0);temp2 = temp1 - (__int64)temp1;printf("%d\n", (int)pow(10, temp2));}return 0;}</span>



1 0