到目前为止,我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main()
{
int i;
int rollDice;
int firInp;
int secInp;
printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=1): ");
scanf("%d", &firInp);
printf("Enter the amount of throws you want(MAX=499, MIN=1): ");
scanf("%d", &secInp);
srand ( time(NULL) );
if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){
for(i = 0; i < secInp; i++){
rollDice = (rand()%firInp) + 1;
printf("%d \n", rollDice);
}
}
else{
printf("Sorry, these numbers don't meet the parameters. Please enter a number in the right parameters.");
}
return 0;
}
我想在代码中包含百分比。我认为这样做的方法是先将代码输出输入数组。如果还有其他方法,请随时告诉我。
编辑:我希望输出是这样的:
1 3 4 4 4 5发生1:16.6%发生3:..依此类推
您可以创建一个整数数组,其大小等于您的骰子可以输出的可能值的数量。然后将其用作获得的出现次数的计数器,数组的索引将表示该输出值(可以使用rollDice-1,因为0不可能是骰子的输出),并且该值位于索引将是发生的次数。完成掷骰子后,只需打印如下百分比:
for (int i=0;i<firInp;i++) { // firInp: n_faces = n_possible_values
printf("Occurrence of %d: %.1f percent\n", i+1, (float)((array[i]*100)/secInp));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句