【UVA 1636】 (条件概率)
题链:https://cn.vjudge.net/problem/UVA-1636
题意
给出一把枪的子弹序列,已知开了一枪且无子弹,问下一步的决策是旋转,还是开下一枪
题解
已知开一枪没子弹,下一枪也没子弹概率设为p1
那么:设P(A)为下一枪无子弹,P(B)为上一枪无子弹。根据条件概率公式
\(P(A|B)=P(AB)\div P(B)\)
即连续两枪无子弹概率除以上一枪无子弹概率,即
\((numof("00")\div n)\div (numof("0")\div n)\)
即
\((numof("00")\div (numof("0")\)
ROTATE无子弹的概率是一个古典概型,即
\(p2=(numof("0")\div n\)
参考代码
import java.io.*;
import java.util.*;
public class Main {
static final int N=(int)5005;
static char a[]=new char[105];
public static void main(String[] args) {
InputStream sys=System.in;
InputReader in=new InputReader(sys);
Scanner sc=new Scanner(new InputStreamReader(sys));
PrintWriter out=new PrintWriter(System.out);
while(sc.hasNext()) {
String str=sc.next();
int c1=0,c2=0;
a=str.toCharArray();
int n=a.length;
for(int i=0;i<n;i++) {
if(a[i]=='0'&&a[(i+1)%n]=='0') {
c1++;
}
if(a[i]=='0') c2++;
}
if(c1*n==c2*c2) out.println("EQUAL");
else if(c1*1.0/c2>c2*1.0/n) out.println("SHOOT");
else out.println("ROTATE");
out.flush();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}