例10-9 uva1636简单概率问题

题意:一个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
");
        else if(shoot < rotat)
            printf("ROTATE
");
        else
            printf("EQUAL
");
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/Przz/p/5409714.html