震惊! Leftmost Digit
題目
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.
分析:
對一個數num可寫為 num=a*10^n , 即科學計數法,使a的整數部分即為num的最高位數字
k^k=a*10^n
如何求a的值?
a=k^k/10^n
如何求n的值?
把n提出來,lga=k*lgk-n
n是整數,而且0 < k* lgk-n< 1, 這說明n是k * lgk的整數部分,因此n=(int)k*lgk
因此a=10^(k*lgk-(int)k *lgk)
實際上,由于k*lgk小數點后有許多數,而int 只有32 位,那么取整用(long long)
代碼
#include<stdio.h> #include<math.h> int main(){int n;double x,a;scanf("%d",&n);while(n--){scanf("%lf",&a);x=a*log10(a);printf("%d\n",(int)pow(10.0,x-(long long)x));} }總結
以上是生活随笔為你收集整理的震惊! Leftmost Digit的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 震惊! Rightmost Digit
- 下一篇: oracle数据库主键消失,oracle