C语言编写一个程序,一个袋子有红,黄,绿,蓝,白五个球,每次抓一个(有放回的抓)连续抓3次,问至少

Python015

C语言编写一个程序,一个袋子有红,黄,绿,蓝,白五个球,每次抓一个(有放回的抓)连续抓3次,问至少,第1张

我想,这道题是求频率吧,那么模拟就可以了,不过要用到随机数。

#include <stdio.h>

#include <math.h>

#include <time.h>

#define ROUNDS 100   // 回合数

#define RED    0

#define YELLOW 1

#define GREEN  2

#define BLUE   3

#define WHITE  4

int main()

{

    int ran, round, i, catched, succ = 0

    srand(time(NULL))

    for (round = 0 round < ROUNDS ++round)

    {

        catched = 0

        for (i = 0 i < 3 ++i)

        {

            ran = rand() % 5

            if (ran == RED)

            {

                catched = 1

            }

        }

        if (catched)

        {

            ++succ

        }

        printf("第%03d轮: %s\n", round, catched ? "抓到红球":"未抓到红球")

    }

    printf("\n共摸球%d次,至少摸到一个红球%d次,频率为:%.2f\n",

            ROUNDS, succ, (double)succ/ROUNDS)

    return 0

}

第一,直接使用硬件来复位,将原本要接到P3.3的电平接到硬件复位上,当然需要一定的设计

第二,还是硬件,直接将此电平接到看门狗电路上

第三,软件,前提是你的单片机支持软件复位(包括直接复位和内置的看门狗)