抛硬币的模拟

#include<stdio.h>
#include<stdlib.h>

#define CALLOC(P, N, S)
  if(!((P)=calloc(N, S))){
	printf( stderr, "Insufficient memory");
	exit(EXIT_FAILURE);
  }

int heads(void);
  
int main(void)
{
	int *F;
	int N,M,cnt=0;
	
	printf("输入每个周期次数:");
	scanf("%d", &N);
	getchar();
	printf("输入有多少个周期:");
	scanf("%d", &M);
	getchar();
		
	CALLOC(F, (N+1), sizeof(int));
	
//	for(int j=0; j<=N; j++)
//		F[j]=0;
    for(int i=0; i<M; i++, F[cnt]++)
    {
    	cnt=0;
		for(int j=0; j<=N; j++)
		    if(heads())
		       cnt++;
    }

	for(int j=0; j<=N; j++)
	{
		printf("%2d ", j);
		for(int i=0; i<F[j]; i+=10)
			putchar('*');
		putchar('
');
	}
	
	free(F);
	
	return 0;
}
int heads(void)
{
	return rand()<RAND_MAX/2;
}

原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/9732299.html