作业帮 > 综合 > 作业

【C语言编程】求解该简单程序的简化方式

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/05/30 16:06:39
【C语言编程】求解该简单程序的简化方式
程序目的:定义一个八面的骰子,让其被抛掷10000次,计算结果每种情况,并求出最大出现数字是哪一面,这下面是我写的,我想知道如何让这个程序变得最简单
#include
#include
int main(void)
{
\x09int frequency1 = 0;
\x09int frequency2 = 0;
\x09int frequency3 = 0;
\x09int frequency4 = 0;
\x09int frequency5 = 0;
\x09int frequency6 = 0;
\x09int frequency7 = 0;
\x09int frequency8 = 0;
\x09int roll;
\x09int face;
\x09for (roll = 1; roll max) { max = frequency2; }
\x09if (frequency3 > max) { max = frequency3; }
\x09if (frequency4 > max) { max = frequency4; }
\x09if (frequency5 > max) { max = frequency5; }
\x09if (frequency6 > max) { max = frequency6; }
\x09if (frequency7 > max) { max = frequency7; }
\x09if (frequency8 > max) { max = frequency8; }
\x09if (max == frequency1) { printf("\nThe highest frequency of dire is face1\n\n"); }
\x09if (max == frequency2) { printf("\nThe highest frequency of dire is face2\n\n"); }
\x09if (max == frequency3) { printf("\nThe highest frequency of dire is face3\n\n"); }
\x09if (max == frequency4) { printf("\nThe highest frequency of dire is face4\n\n"); }
\x09if (max == frequency5) { printf("\nThe highest frequency of dire is face5\n\n"); }
\x09if (max == frequency6) { printf("\nThe highest frequency of dire is face6\n\n"); }
\x09if (max == frequency7) { printf("\nThe highest frequency of dire is face7\n\n"); }
\x09if (max == frequency8) { printf("\nThe highest frequency of dire is face8\n\n"); }
\x09system("pause");
\x09return 0;
}
【C语言编程】求解该简单程序的简化方式
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    int count[9]={0};//1,2,3,...8面
\x09int i,randvalue,maxcount,maxflag;
\x09srand(time(NULL));
\x09for(i=1;i<=10000;i++)
\x09{
\x09\x09randvalue = rand()%8+1;
\x09\x09count[randvalue]++;
\x09}
    maxcount=count[1];
\x09maxflag=1;
\x09for(i=1;i<=8;i++)
\x09{
\x09\x09if(maxcount<count[i])
\x09\x09{
\x09\x09\x09maxcount = count[i];
\x09\x09\x09maxflag =i;
\x09\x09}
\x09}
\x09printf("%d %d\n",maxflag,maxcount);
\x09return 0;
}