投掷骰子,编制一个输出每一面的程序。骰子的面是是随机生成的。
制造抛掷骰子用函数get_dice_ face(),
在这个函数中每一个面出现的时候,利用静态区域变量来记住其次数 。
get dice face()调用次数达到100时,输出各方面的次数。
备注:骰子面的0到5的整数,从0到5的随机数可以生成rand()%6。
谢谢大佬!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int get_dice_face()
{
static int times = 0;
static int stat[6] = {0,0,0,0,0,0};
stat[rand() % 6]++;
times++;
if (times == 100)
{
for (int i = 0; i < 6; i++)
{
printf("%d -> %d\n", i + 1, stat[i]);
}
}
return times;
}
int main()
{
while (get_dice_face() < 100);
return 0;
}