hdu 1060 Leftmost Digit

来源:互联网 发布:python中的iter 编辑:程序博客网 时间:2024/06/05 11:53
为保证安全,携程编程大赛期间4月11日18:00~21:00将关闭比赛外的其他模块,带来不便,敬请谅解。

Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12283    Accepted Submission(s): 4695


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
 

Recommend
We have carefully selected several similar problems for you:  1061 1071 1573 1066 1065 
数学题,求n的n次方结果中最左边的一位数
幸好前几天胜哥考了我一个问题:求n!的位数
解法是用log10(n!)==log10(n)+log10(n-1)+...+log10(1);
又遇到类似题我就很自然地想到了用log
不妨设x == log10(n^n) == n*log(n);
则10^x == n^n,就转化为求10的x次方最左边一位的值
这样只需取出x的小数部分即可

虽然一开始思路就是对的,但是CE了好多次,发现自己每次用log总是各种错,但是编译器又不报错。。。
代码如下:
#include <map>#include <cmath>#include <vector>#include <string>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define esp 1e-9#define MAXN 10010#define ll long long#define INF 0x7FFFFFFF#define BUG system("pause")#define SW(a,b) a^=b;b^=a;a^=b;using namespace std;int main(void){ll t;double n;cin >> t;while(t--){cin >> n;if(n){double x = n*log10((double)n);ll ts = x;x -= ts;ll w = (ll)pow(10.0,x);cout << w << endl;}elsecout << "1" << endl;}return 0;}


 
0 0