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

猜数字

时间:2016-04-18 11:43:33      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description
A有1数m,B来猜.B每猜一次,A就说&quot;太大&quot;,&quot;太小&quot;或&quot;对了&quot; 。 <br>问B猜n次可以猜到的最大数。 <br>
 
Input
第1行是整数T,表示有T组数据,下面有T行 <br>每行一个整数n (1 ≤ n ≤ 30) <br>
 
Output
猜n次可以猜到的最大数<br>
 
Sample Input
2
1
3
 
Sample Output
1
7
 
 
思路:开始并没有弄清楚这道题想要干什么,然后参考了其他人的解析。
  我们知道这道题的猜法,如果我们猜1-m最多猜测(int)log2(m)+ 1= n次就一定可以猜出其中的所有的数,那么我们可以得到猜测的最大值为
  m = 2^n-1,易得问题解
 
代码:

#include <iostream>
#include<math.h>
using namespace std;

int main()
{
  int T = 0;
  int n = 0;
  while(cin >> T){
    while(T-- && cin >> n){
      cout << (int)pow(2,n)-1 << endl;
    }
  }
  return 0;
}

猜数字

标签:

原文地址:http://www.cnblogs.com/2016zhanggang/p/5403426.html

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