2^n的第一位数字 soj 3848 mathprac

简介:
 Time Limit: 3000 MS    Memory Limit: 65536 K 



                                                      mathprac

Description



One lovely afternoon, Bessie's friend Heidi was helping Bessie
review for her upcoming math exam.

Heidi presents two integers A (0 <= A <= 45) and B (1 <= B <= 9)
to Bessie who must respond with an integer E in the range 1..62.
E is the smallest integer in that range that is strictly greater
than A and also has B as the first digit of 2 raised to the E-th
power. If there is no answer, Bessie responds with 0.

Help Bessie correctly answer all of Heidi's questions by calculating
her responses.

By way of example, consider A=1 and B=6. Bessie might generate a table
like this:
         E         2^E    First digit of 2^E
         2          4            4
         3          8            8
         4         16            1
         5         32            3
         6         64            6      <-- matches B

Thus, E=6 is the proper answer.

NOTE: The value of 2^44 does not fit in a normal 32-bit integer.

Input



* Line 1: Two space-separated integers: A and B

Output



* Line 1: A single integer E calculated as above

Sample Input



1 6

Sample Output



6

题意:

就是求2^n的第一位数字;

由于2^n=t*10^k,那么也就是求t的第一位。

log10(2^n)=n*log10(2)

                =log10(t*10^k)

                =log10(t)+k   k为整数

求出n*log10(2)减去k即可求得log10(t),然后求出10^log10(t),对其取整,即为所求。

复制代码
 
 
#include < iostream >
#include
< math.h >
using namespace std;

int main( void )
{
int A,B;
while (scanf( " %d%d " , & A, & B) == 2 )
{
int i;
double d;
int digit;
int p;
for (i = A + 1 ;i <= 62 ;i ++ )
{
d
= i * log10( 2.0 );
p
= ( int )(d);
digit
= ( int )pow( 10.0 ,d - p);
if (digit == B)
{
printf(
" %d\n " ,i);
break ;
}
}
if (i == 63 )
printf(
" 0\n " );
}
return 0 ;
}
复制代码


本文转载自海 子博客园博客,原文链接: http://www.cnblogs.com/dolphin0520/archive/2011/04/11/2012982.html 如需转载自行联系原作者
相关文章
|
5月前
数一下 1到 100 的所有整数中出现多少个数字9并输出这些数字
数一下 1到 100 的所有整数中出现多少个数字9并输出这些数字
66 0
|
5月前
|
Python
如果一个n位正整数等于其各位数字的n次方之和
如果一个n位正整数等于其各位数字的n次方之和
|
3月前
|
容器
只出现一次的数字
只出现一次的数字
21 0
|
4月前
|
人工智能 安全 算法
数字文盲
在数字时代,避免边缘化的关键是正视挑战、持续学习数字技能、关注伦理安全和推广数字素养教育。从基础操作到数据分析,提升数字素养能帮助我们适应变革,同时保护隐私和安全,确保在科技发展中不落伍,共建包容的数字社会。
|
5月前
28.求任意一个整数的十位上的数字
28.求任意一个整数的十位上的数字
73 3
|
5月前
|
机器学习/深度学习
判断一个数字是否是2的N次方
判断一个数字是否是2的N次方
35 0
|
5月前
有多少小于当前数字的数字
有多少小于当前数字的数字
25 1
|
5月前
|
算法 C++
只出现一次的数字(C++)
只出现一次的数字(C++)
32 0
|
5月前
|
C++
(C++)只出现一次的数字I--异或
(C++)只出现一次的数字I--异或
31 0
|
5月前
|
C++
(C++)只出现一次的数字II--异或
(C++)只出现一次的数字II--异或
44 0