首页 > 试题广场 >

编写一个程序,该程序与我们在显示程序清单12.13的输出之后

[问答题]

编写一个程序,该程序与我们在显示程序清单12.13的输出之后所讨论的修改版程序具有相同表现。也就是说,输出应像下面这样:

Enter the number of sets; enter q to stop.

18

How many sides and how many dice?

6 3

Here are 18 sets of 3 6-sided throws.

12  10   6   9   8  14   8  15   9  14  12  17  11   7  10

13   8  14

How many sets? Enter q to stop.

q

推荐
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
 char set,side,dice,i,sum;
 puts("Enter the number of sets:enter q to stop");
 while(scanf("%d",&set) == 1)
 {
 srand( time(0) );
 puts("How many sides and how many dice?");
 while(scanf("%d%d", &side, &dice) != 2)
 {
 scanf("%*s");//滤除非法输入
 printf("input error! input again:");
 }
 printf ("Here are %d sets of %d %d-sided throws.\n",set, dice, side);
 while (set--)
 {
 for (i=0,sum=0; i<dice; i++)
 sum += rand() % side +1;
 printf("\t%d",sum);
 }
 puts("\nHow many sets?enter q to stop");
 }
 return 0;
}

发表于 2018-05-05 21:55:47 回复(0)