ACM Steps 2.1.7

Leftmost Digit   Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)  Total Submission(s): 3377 Accepted Submission(s): 1485

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.

Author

Ignatius.L 题意: 求N^N的最左边那位数。 思路: 从网上找到的一个公式:N^N=10^(log10(N^N))=10^(N*log10(N)),此结果可以分为10的整数次幂乘以10的小数次幂,这就转换为求10的小数次幂中 第一个数。(至于10的小数次幂结果大于1小于10。。。。。。。。。。)

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
using namespace std;
int main()
{
    int t;
    double a,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf",&a);
        b=a*log10(a);
        long long c=b;
        b=b-c;
        b=pow(10,b);
        int d=b;
        printf("%d\n",d);
    }
    return 0;
}

上一篇:洛谷 P2661 信息传递(并查集 & 最小环)


下一篇:Selenium测试Ajax程序(转)