Leftmost Digit

来源:互联网 发布:工作室监控软件 编辑:程序博客网 时间:2024/06/05 09:14

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
234
 
Sample Output
22
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,用科学技术法表示然后在对系数取整即可。


#include<iostream>
#include<cmath>
using namespace std;


int n;


int main(){
    int a;
    long double f;
    double x;
    while(cin>>n){
        while(n-->0){
            cin>>a;
            f=a*log10(a*1.0)-(long long)(a*log10(a*1.0));
            long double d=10;
            x=pow(d,f);
            cout<<int(x)<<endl;
        }
    }
    return 0;
}



0 0