例10-9 uva1636简单概率问题

来源:互联网 发布:华为云计算部门效益 编辑:程序博客网 时间:2024/06/15 04:24
题意:一个01串,0代表没子弹,1代表有子弹。在开一次空枪后,开下一枪没子弹概率大的方案
①接着开枪    ②随机转一下再开枪

思路:

在情况一就是求00在0中占的比例,情况二则是0在整个串中的比例

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <cstdlib>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 105;char p[maxn];int main(){    while(scanf("%s",p) != EOF)    {        int len =strlen(p);        int anum = 0;        int tnum;        p[len] = p[0];        for(int i = 0; i < len; i++)        {            if(p[i] == '0')                anum++;        }        double rotat = (double)anum / len;         tnum = 0;        for(int i = 0; i < len; i++)        {            if(p[i] == '0' && p[i+1] == '0')                tnum ++;        }        double shoot = (double)tnum/anum;        if(shoot > rotat)            printf("SHOOT\n");        else if(shoot < rotat)            printf("ROTATE\n");        else            printf("EQUAL\n");    }    return 0;}



0 0
原创粉丝点击