猜数字
Problem Description
A有1数m,B来猜.B每猜一次,A就说"太大","太小"或"对了" 。 <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;
}

浙公网安备 33010602011771号