题目:
猜数字 |
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 318 Accepted Submission(s): 247 |
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 |
Author
Zhousc
|
Source
ECJTU 2008 Summer Contest
|
Recommend
lcy
|
题目分析:
要猜到最大的数字m,也就是说,在1到m间的每一个数,你都能在n次内把它猜出来!
所以说在最坏的情况下,在1到m间,你最多只要猜log2(m+1)(取整)次,所以易知==>m=2^n - 1.即猜n次,你能猜到的最大的数为2^n - 1.我们也可认为,在数1到2^n - 1间,我们都可以在n次内猜出来,如果大于这个数,n次内我们就有 可能猜不来了。例如1--7间,我们至少要猜3次猜可以
代码如下:
/*
* i.cpp
*
* Created on: 2015年2月16日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
printf("%d\n",(int)pow(2 + 0.0,n)-1);
}
return 0;
}