猜数字
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3561 Accepted Submission(s): 2553
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
#include <stdio.h>
#include <math.h>
int main()
{
int times,n;
scanf("%d",×);
while (times--)
{
scanf("%d",&n);
printf("%.lf\n",pow(2,n)-1);
}
}
这道题,原来我也是一塌糊涂!现在好多了!
首先看到像这样的题,我们就会想到二分法!他没有给你M的值,所以给我们的第一感觉就是无从下手,其实你仔细一看他不就是一个求方程的解M吗?
假设n是4,我们求M的值
((((1+M)/2+1+M)/2+1+M)/2+1+M)/2=M
我们来解M的值!
(((1+M)/2+1+M)/2+1+M)/2+1+M=2*M
((1+M)/2+1+M)/2+1+M)/2=2*M-M-1=M-1
((1+M)/2+1+M)/2+1+M=2*(M-1)
((1+M)/2+1+M)/2=2*(M-1)-M-1=M-3
(1+M)/2+1+M=2*(M-3)
(1+M)/2=M-7
M=15
这不就是2的4次方-1吗?
找到规律了吗?