HOJ Leftmost Digit

来源:互联网 发布:java获取select的值 编辑:程序博客网 时间:2024/05/19 06:16

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=11

Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3530 Accepted Submission(s): 1568 
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
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.
 
Author
Ignatius.L

把一个数字表示成科学计数法,用科学计数法求最高位。


n^n=m;

nlgn=lg(a*10^b)   1<=a<10

nlgn=lga+b

lga 为nlgn小数部分,b为nlgn整数部分

lga=nlgn-(long long)(nlgn)



#include <iostream>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <map>#include <algorithm>#include <vector>#include <string>#include <cstring>#include <sstream>using namespace std;long long n;int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%I64d",&n);        double tmp=n*1.0*log10(n)-(long long )(n*1.0*log10(n));        double t1=pow(10,tmp);        double ans=floor(t1);        printf("%.0f\n",ans);    }    return 0;}


0 0