B题 HDU1060
Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22158 Accepted Submission(s): 8577
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.
解题思路:
1、m=n^n,要取m的最高位;
2、log10(m)=log10(n^n);
3、m=10^(n*log10(n));
4、设n*log10(n)=a+b; (a为整数部分,b为小数部分)
5、则m=10^(a+b)=(10^a)*(10^b);
6、由于10^a的最高位是1,不影响结果,所以m的最高位只需考虑10^b;
7、最后对10^b的结果取整;(由于0<b<10,所以1<10^b<10,即为所求结果)
题目代码:
#include <bits/stdc++.h> typedef long long LL; using namespace std; int main(){ int n,t; double x,y; LL result; cin>>t; while(t--){ cin>>n; x=n*log10(n); y=LL(n*log10(n)); x=x-y; result=(LL)pow(10.0,x); cout<<result<<endl; } return 0; }