1 #include <stdlib.h>
2
3 int heads(){
4 return rand() < RAND_MAX/2;
5 }
6
7 void main(int argc, char *argv[]){
8 int i, j;
9 int count;
10 int N = atoi(argv[1]);
11 int M = atoi(argv[2]);
12 int *f = malloc((N+1)*sizeof(int));
13 for(j=0; j<=N; j++){
14 f[j] = 0;
15 }
16 for(i=0; i<=M; i++){
17 count = 0;
18 for(j = 0; j <= N; j++){
19 if(heads()){
20 count++;
21 }
22 }
23 f[count]++;
24 }
25 for(j=0; j<=N; j++){
26 printf("%2d", j);
27 for(i = 0; i<f[j]; i += 50){
28 printf("*");
29 }
30 printf("\n");
31 }
32 }
算法c语言实现 3-7 抛硬币模拟
最新推荐文章于 2023-03-09 23:03:39 发布