70,035
社区成员
发帖
与我相关
我的任务
分享
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
int roll_count = 0;
int rollem(int sides);
int roll_n_dice(int dice, int sides);
int main(void)
{
int dice, roll;
int sides;
srand((unsigned int)time(0));
printf("Enter the number of sides per die, 0 to quit.\n");
while(scanf("%d", &sides)==1 && sides>0)
{
printf("how many dice?\n");
scanf("%d", &dice);
roll = roll_n_dice(dice, sides);
printf("You have rolled a %d using %d %d-sided dice.\n",
roll, dice, sides);
printf("How many sides? Enter 0 to quit.\n");
}
printf("The rollem() function was called %d times.\n", roll_count);
printf("GOOD FOUTURN TO YOU!\n");
return 0;
}
int rollem(int sides)
{
int roll;
roll = rand()%sides+1;
++roll_count;
return roll;
}
int roll_n_dice(int dice, int sides)
{
int d;
int total = 0;
if(sides < 2)
{
printf("Need at least 2 sides.\n");
return -2;
}
if(dice < 1)
{
printf("Need at least 1 die.\n");
return -1;
}
for(d=0;d<dice;d++)
{
total += rollem(sides);
}
return total;
}


#include "stdio.h"
#include "stdlib.h"
#include "time.h"
int roll_count = 0;
int rollem(int sides);
int roll_n_dice(int dice, int sides);
int main(void)
{
int dice, roll;
int sides;
srand((unsigned int)time(0));
printf("Enter the number of sides per die, 0 to quit.\n");
while(scanf("%d", &sides)==1 && sides>0)
{
printf("how many dice?\n");
scanf("%d", &dice);
roll = roll_n_dice(dice, sides);
printf("You have rolled a %d using %d %d-sided dice.\n",roll, dice, sides);
printf("How many sides? Enter 0 to quit.\n");
}
printf("The rollem() function was called %d times.\n", roll_count);
printf("GOOD FOUTURN TO YOU!\n");
return 0;
}
int rollem(int sides)
{
int roll;
roll = rand()%sides+1;//这里是用了随机的。。本来就是模拟掷骰子。。当然每次结果不一样。。你把这句改成roll = 1;保证每次结果都一样。。
++roll_count;
return roll;
}
int roll_n_dice(int dice, int sides)
{
int d;
int total = 0;
if(sides < 2)
{
printf("Need at least 2 sides.\n");
return -2;
}
if(dice < 1)
{
printf("Need at least 1 die.\n");
return -1;
}
for(d=0;d<dice;d++)
{
total += rollem(sides);
}
return total;
}