c语言编程:模拟骰子的6000次投掷,编程统计并输出其6个面各自出现的概率。求各路高手帮忙,先谢过了

如题所述

#include <stdio.h>
#include<stdlib.h>
#include <time.h>

 main()
{
    int  face;   // 储存每次色子的点数
int roll;  //投掷色子的次数(循环变量)
int frequency[7] = {0};  //记录色子每个点数出现的次数

    srand(time (NULL));  //用系统时间来初始化系统随机数的种子值

     //用循环产生6000次随机数,并记录每个点数出现的次数
    for (roll=1; roll<=6000; roll++)
    { 
        face = rand()%6 + 1; 
        frequency[face]++;
    }
     
    printf("%4s%17s\n", "Face", "Frequency");
     
//输出每个点数出现的次数
    for (face=1; face<=6; face++)
    {
        printf("%4d%17d\n", face, frequency[face]);
    }

system("pause");
 }

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-02-28
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 6000
int main()
{
static int r[6]={0};
int i;
srand(time(0));
for(i=0;i<N;i++)
r[rand()%6]++;
for(i=0;i<6;i++)
printf("%d点:%5d次 %2.3f%%\n",i+1,r[i],(double)r[i]/N*100);
return 0;
}本回答被网友采纳
第2个回答  2012-05-08
#include <time.h>
#include <iostream>
using namespace std;

int main()
{
srand(time(0));
int a[6]={0};
for (int i=0; i<6000; i++)
{
switch (rand()%6)
{
case 0:
a[0]++;
break;
case 1:
a[1]++;
break;
case 2:
a[2]++;
break;
case 3:
a[3]++;
break;
case 4:
a[4]++;
break;
case 5:
a[5]++;
break;
}
}
for(int i=0; i<=5;i++)
cout<<"the odds of "<<i<<" is "<<a[i]/6000<<endl; // 0出现的概率即为6出现的概率
return 0;
}
第3个回答  2012-05-08
#include <time.h>
#include <iostream>
using namespace std;

int main()
{
srand(time(0));
int n1=0,n2=0,n3=0,n4=0,n5=0,n6=0;
for (int i=0; i<6000; i++)
{
switch (rand()%6)
{
case 0:
n1++;
break;
case 1:
n2++;
break;
case 2:
n3++;
break;
case 3:
n4++;
break;
case 4:
n5++;
break;
case 5:
n6++;
break;
}
}
cout<<n1<<" "<<n2<<" "<<n3<<" "<<n4<<" "<<n5<<" "<<n6<<endl;
return 0;
}
第4个回答  2012-05-08
顶楼上
相似回答