- 论坛徽章:
- 0
|
- #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);
- } while(ch != 1 && ch != 2 && ch != 3);
- ch--;
- srand(time(NULL));
- do {
- x[1] = 1 + (int)( 3.0 * ( rand() / (RAND_MAX + 1.0) ) );
- x[2] = 1 + (int)( 3.0 * ( rand() / (RAND_MAX + 1.0) ) );
- x[0] = 1 + (int)( 3.0 * ( rand() / (RAND_MAX + 1.0) ) );
- } 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)( 2.0 * ( rand() / (RAND_MAX + 1.0) ) );
- other = (int)( 2.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);
- } while(i != ch+1 && i != other+1);
- i--;
- if(i != max)
- puts("You lose, a goat behind this door");
- else
- puts("You win, a car behind this door");
- return EXIT_SUCCESS;
- }
- #include<time.h>
- #include<stdio.h>
- #include<stdlib.h>
- int main(void)
- {
- int ch,max,open,other;
- srand(time(NULL));
- for(;;) {
- open = 1 + (int)( 3.0 * ( rand() / (RAND_MAX + 1.0) ) );
- ch = 1 + (int)( 3.0 * ( rand() / (RAND_MAX + 1.0) ) );
- max = 1 + (int)( 3.0 * ( rand() / (RAND_MAX + 1.0) ) );
- other = 1 + (int)( 3.0 * ( rand() / (RAND_MAX + 1.0) ) );
- if(other != ch && other != open && open != ch && open != max) {
- printf("ch=%d open=%d other=%d max=%d\n", ch,open,other,max);
- break;
- }
- }
- return EXIT_SUCCESS;
- }
复制代码 |
|