题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2178
题 意:任意给你一个数字,问最多要猜多少次才能猜到。
思 路:用二分的思想,在1到m中猜k 对m二分求值进行比较,最坏的情况下要比较(int)log2(m)+1次,所以猜到k的最坏情况为k=2^n-1.
即猜n时,猜测的数不超过2^n-1也可认为在数1到2^n-1之间都可以在n次内猜出。
代码如下:
#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <algorithm>
int main()
{
int T;
scanf ( "%d", &T );
while( T-- )
{
int n;
scanf ( "%d", &n );
printf("%d\n",(int)pow(2,n)-1);
}
return 0;
}