Leftmost Digit
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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).
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.
题目大意:
给你一个 n ,然后求出 n ^ n 的最高位的数值是多少。
对一个数num可写为 num=10n + a, 即科学计数法,使a的整数部分即为num的最高位数字numnum=10n + a 这里的n与上面的n不等两边取对数: num*lg(num) = n + lg(a);因为a<10,所以0<lg(a)<1令x=n+lg(a); 则n为x的整数部分,lg(a)为x的小数部分又x=num*lg(num);a=10(x-n) = 10(x-int(x)))再取a的整数部分即得num的最高位
附上代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
double n;
scanf("%lfd",&n);
double a = n * log10(n);
long long int b = (long long int)a;
double c = a - b;
long long int ans = (long long int)pow(10,c);
printf("%I64d\n",ans);
}
return 0;
}