car-n-goat.c

来自「自己做的常用库和实现的数据结构。public domain.」· C语言 代码 · 共 54 行

C
54
字号
/* Demo of car and goat game, using random number generator. * * Written by Cyril Hu (cyrilhu@gmail.com), public domain. */#include<ctype.h>#include<stdio.h>#include<time.h>#include<stdlib.h>int main(void){	int i, other, open, max, ch, x[3];	do {		puts("Choose one door, 1,2,3?");		scanf("%d", &ch);		ch--;	} while(ch != 1 && ch != 2 && ch != 0);	srand(time(NULL));	do {		x[0] = rand();		x[1] = rand();		x[2] = rand();	} while(x[1]==x[2] || x[0]==x[1] || x[0]==x[2]);	for(max=i=0; i<3; i++) {		if(x[max] < x[i])			max = i;	}	for(;;) {		open = (int)(  3.0 * ( rand() / (RAND_MAX + 1.0) )  );		other = (int)(  3.0 * ( rand() / (RAND_MAX + 1.0) )  );		if(other != ch && other != open && open != ch && open != max) {			printf("Door %d is open, a goat behind it\n", open+1);			break;		}	}	do {		printf("Now, Door %d or Door %d ?\n", ch+1, other+1);		scanf("%d", &i);		i--;	} while(i != ch && i != other);	if(i != max)		puts("You lose, a goat behind this door");	else		puts("You win, a car behind this door");	return EXIT_SUCCESS;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?