猜数字

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;
}

posted @ 2016-04-18 10:20  SDAU_ZG  阅读(205)  评论(0)    收藏  举报