HDOJ2178

http://acm.hdu.edu.cn/showproblem.php?pid=2178

猜数字

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1864    Accepted Submission(s): 1348


Problem Description
A有1数m,B来猜.B每猜一次,A就说"太大","太小"或"对了" 。 
问B猜n次可以猜到的最大数。 
 

Input
第1行是整数T,表示有T组数据,下面有T行 
每行一个整数n (1 ≤ n ≤ 30) 
 

Output
猜n次可以猜到的最大数
 

Sample Input
   
   
   
   
2 1 3
 

Sample Output
   
   
   
   
1 7
做这题,起初还没读懂什么意思 ,,,,,A有1数m,B来猜.B每猜一次,A就说"太大","太小"或"对了" 。
问B猜n次可以猜到的最大数。 ,,,,,首先,, 设猜到的最大的数字为h,,猜到最大的数字h,也就是
说,在1到h间的每一个数,你都能在m次内把它猜出来!
所以说在最坏的情况下,在1到h间,你最多只要猜log2(h)+1(取整)次,所以易知==>h=2^m-1.即猜m次,
能猜到的最大的数为2^m-1。


#include<iostream>
#include<cmath>

using namespace std;

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



你可能感兴趣的:(HDOJ2178)