码迷,mamicode.com
首页 > 其他好文 > 详细

Leftmost Digit(hdu1060)(数学题)

时间:2016-10-15 19:52:25      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

Leftmost Digit

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


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.
 
//这题不难读懂,就是说 N^N 的最高位是什么数字,第一行 t 代表测试数据组数
显然这不是模拟能解决的n有10亿,那么就肯定是数学题了
   e = log10(N^N) = N * log10(N) (对数公式)
那么 10^e == N^N 然后想想 10^floor(e) 等于什么呢,不就是与 N^N 相同的位数,但最小的数吗?就是 1 后面都是 0 的数
而 floor( 10^(e-floor(e)) ) 就是就是要求的了
 
技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int t;
10     int n;
11     scanf("%d",&t);
12     while(t--)
13     {
14         scanf("%d",&n);
15         double temp=n*log10(n*1.0);
16         double res=temp-floor(temp);
17         printf("%d\n",(int)pow(10.0,res));
18     }
19     return 0;
20 }
View Code

 

 
 

Leftmost Digit(hdu1060)(数学题)

标签:

原文地址:http://www.cnblogs.com/haoabcd2010/p/5964838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!