猜数字
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9491 Accepted Submission(s): 6438
Problem Description
A有1数m,B来猜.B每猜一次,A就说"太大","太小"或"对了" 。
问B猜n次可以猜到的最大数。
问B猜n次可以猜到的最大数。
Input
第1行是整数T,表示有T组数据,下面有T行
每行一个整数n (1 ≤ n ≤ 30)
每行一个整数n (1 ≤ n ≤ 30)
Output
猜n次可以猜到的最大数
Sample Input
2 1 3
Sample Output
1 7
题意:比如 1 3,用2次就可以猜出,1 7,用3次可以猜出,7就是n次可以猜出的最大值。就是个二分的逆过程
代码上的总结:还是喜闻乐见的pow(2,n)会WA,不过改良的办法是(int)pow(2,n),pow是会有精度上的小丢失的。
另外一个就是用位运算来表示 1<<n
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
int main(void)
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
cout<<((1<<n)-1)<<endl;
//cout<<((int)(pow(2,n))-1)<<endl;
}
return 0;
}