UVA.1636 Headshot (概率)
标签: 概率
题意分析
已知第一枪打空了,并且已知子弹序列,现在求解为了使下一枪不打空,现在是继续开一枪好,还是随机转动一下再开枪好,或者两者相等。
为了求解下一次是直接开枪,还是随机转动一下,就要计算出直接开枪放空的概率和随机转动后放空的概率。
开枪放空概率
由于第一枪是放空(设为事件A)的,所以现在的问题是下一枪(设为事件B)也放空的概率是多少。明显是条件概率。
P(B|A)=P(AB)P(A)
第一枪放空对应一个0,第二枪放空对应00,故可以得出。
P(B|A)=P(AB)P(A)=P(00)P(0)
.
遍历序列,统计出00和0的个数即可。
随机转动后放空的概率
考虑什么时候会随机转动下一枪会放空。那只能是随机转动到没有子弹的位置,所以也很容易得出。
P=P(0)n
其中n是字符串的总长度,即strlen(str).
然后比较两者大小,做出判断即可
代码总览
#include <bits/stdc++.h>
#define nmax 150
#define eps 1e-4
using namespace std;
char str[nmax];
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(scanf("%s",str) != EOF){
int len = strlen(str);
int posOf00 = 0,posOf0 = 0;
for(int i = 0;i<len-1;++i){
if(str[i] == '0') posOf0++;
if(str[i] == '0' && str[i+1] == '0') posOf00++;
}
if(str[len-1] == '0') posOf0++;
if(str[len-1] == '0' && str[0] == '0') posOf00++;
double posi1 = 1.0 * posOf00 / posOf0;
double posi2 = 1.0 * posOf0 / len;
if(fabs(posi1 - posi2) < eps){
printf("EQUAL\n");
}else{
if(posi1 < posi2){
printf("ROTATE\n");
}else{
printf("SHOOT\n");
}
}
}
return 0;
}