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

[复制链接]
593|6
 楼主| functions 发表于 2019-9-14 17:59 | 显示全部楼层 |阅读模式
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.
 楼主| functions 发表于 2019-9-14 17:59 | 显示全部楼层
Input


* Line 1: Two space-separated integers: A and B
 楼主| functions 发表于 2019-9-14 17:59 | 显示全部楼层
Output


* Line 1: A single integer E calculated as above
 楼主| functions 发表于 2019-9-14 18:00 | 显示全部楼层
Sample Input


1 6

Sample Output


6
 楼主| functions 发表于 2019-9-14 18:00 | 显示全部楼层
题意:

就是求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),对其取整,即为所求。
 楼主| functions 发表于 2019-9-14 18:00 | 显示全部楼层
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;

  4. int main(void)
  5. {
  6.     int A,B;
  7.     while(scanf("%d%d",&A,&B)==2)
  8.     {
  9.         int i;
  10.         double d;
  11.         int digit;
  12.         int p;
  13.         for(i=A+1;i<=62;i++)
  14.         {
  15.             d=i*log10(2.0);
  16.             p=(int)(d);
  17.             digit=(int)pow(10.0,d-p);
  18.             if(digit==B)
  19.             {
  20.                 printf("%d\n",i);
  21.                 break;
  22.             }
  23.         }
  24.         if(i==63)
  25.             printf("0\n");
  26.     }
  27.     return 0;
  28. }
 楼主| functions 发表于 2019-9-14 18:01 | 显示全部楼层
作者:Matrix海子
    
出处:http://www.cnblogs.com/dolphin0520/
    
本博客中未标明转载的**归作者Matrix海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在**页面明显位置给出原文连接,否则保留追究法律责任的权利。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

39

主题

446

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部