免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2388 | 回复: 13
打印 上一主题 下一主题

第一次碰到奇怪的运行时错误。。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-18 12:05 |只看该作者 |倒序浏览
代码是经典的车和羊的实现,就是一个电视游戏,有3道门(1道门后面有汽车,2道后面只有羊),先让玩家任选一道,然后打开另外2道中的非汽车门,(也就是说打开的是一道有羊的门,也即留下一道有汽车的门和一道有羊的门),然后再给玩家一次二选一的机会,打开有汽车的门为胜利。

#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;

}

论坛徽章:
0
2 [报告]
发表于 2007-05-18 12:10 |只看该作者
原帖由 hcq0411 于 2007-5-17 20:05 发表
代码是经典的车和羊的实现,就是一个电视游戏,有3道门(1道门后面有汽车,2道后面只有羊),先让玩家任选一道,然后打开另外2道中的非汽车门,(也就是说打开的是一道有羊的门,也即留下一道有汽车的门和一道有羊 ...


说一下运行时出了什么错误吧。
记得是要换,可以有2/3的概率获胜(不换只有1/3)。

论坛徽章:
0
3 [报告]
发表于 2007-05-18 12:16 |只看该作者
for循环那个地方是个无限循环。

我的代码实现得不好,笑话了。(暂时没想改进的解法,因为最原始的实现,居然运行时都出错了。)

我生成含有3个元素的随机有序数组。 然后设3个变量ch是选择的门,open是被打开的门,other是另外一道,max是藏有机车的门。(我用排序的方法把含有最大元素的门作为获胜门), 这样经过几次随机和判断,应该就实现了吧。

运行出错,体现在ch只能输入3,不能输1,2,否则出错,,,
32/64位(我的环境是fc6 x86_64 gcc4.1)下会让你重新输入,且没有puts那句提示,
dos下用TC2编译后,若输入1,2则直接卡死,当机。

我怀疑是随机数有问题,用下面的代码试了一下,
#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;
}

结果没有问题,ch=1, ch=2, ch=3 都没问题。

我于是没辙了。。。
救我!!!

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2007-05-18 12:40 |只看该作者
代码还是用 [code] [/code] 括起来吧,然后如果有必要,再把 smilies 禁止,要不然看起来真的很费劲。

[[i] 本帖最后由 MMMIX 于 2007-5-18 12:43 编辑 [/i]]

论坛徽章:
0
5 [报告]
发表于 2007-05-18 12:45 |只看该作者
sorry, i just don't know how to use it.   thank you for telling me using
复制代码
, ,i will memorize.

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
6 [报告]
发表于 2007-05-18 12:47 |只看该作者
原帖由 hcq0411 于 2007-5-18 12:45 发表
sorry, i just don't know how to use it.   thank you for telling me using
复制代码
, ,i will memorize.

呃……我的意思是建议你把上面的帖子改下

论坛徽章:
0
7 [报告]
发表于 2007-05-18 13:04 |只看该作者

  1. #include<ctype.h>

  2. #include<stdio.h>

  3. #include<time.h>

  4. #include<stdlib.h>



  5. int main(void)

  6. {

  7.         int i, other, open, max, ch, x[3];



  8.         do {

  9.                 puts("Choose one door, 1,2,3?");

  10.                 scanf("%d", &ch);

  11.         } while(ch != 1 && ch != 2 && ch != 3);

  12.         ch--;



  13.         srand(time(NULL));

  14.         do {

  15.                 x[1] = 1 + (int)(  3.0 * ( rand() / (RAND_MAX + 1.0) )  );

  16.                 x[2] = 1 + (int)(  3.0 * ( rand() / (RAND_MAX + 1.0) )  );

  17.                 x[0] = 1 + (int)(  3.0 * ( rand() / (RAND_MAX + 1.0) )  );

  18.         } while(x[1]==x[2] || x[0]==x[1] || x[0]==x[2]);



  19.         for(max=i=0; i<3; i++) {

  20.                 if(x[max] < x[i])

  21.                         max = i;

  22.         }



  23.         for(;;) {

  24.                 open = (int)(  2.0 * ( rand() / (RAND_MAX + 1.0) )  );

  25.                 other = (int)(  2.0 * ( rand() / (RAND_MAX + 1.0) )  );

  26.                 if(other != ch && other != open && open != ch && open != max) {

  27.                         printf("Door %d is open, a goat behind it\n", open+1);

  28.                         break;

  29.                 }

  30.         }



  31.         do {

  32.                 printf("Now, Door %d or Door %d ?\n", ch+1, other+1);

  33.                 scanf("%d", &i);

  34.         } while(i != ch+1 && i != other+1);

  35.         i--;



  36.         if(i != max)

  37.                 puts("You lose, a goat behind this door");

  38.         else

  39.                 puts("You win, a car behind this door");



  40.         return EXIT_SUCCESS;

  41. }




  42. #include<time.h>
  43. #include<stdio.h>
  44. #include<stdlib.h>

  45. int main(void)
  46. {
  47.         int ch,max,open,other;

  48.         srand(time(NULL));
  49.         for(;;) {
  50.                 open = 1 + (int)(  3.0 * ( rand() / (RAND_MAX + 1.0) )  );
  51.                 ch = 1 + (int)(  3.0 * ( rand() / (RAND_MAX + 1.0) )  );
  52.                 max = 1 + (int)(  3.0 * ( rand() / (RAND_MAX + 1.0) )  );
  53.                 other = 1 + (int)(  3.0 * ( rand() / (RAND_MAX + 1.0) )  );
  54.                 if(other != ch && other != open && open != ch && open != max) {
  55.                         printf("ch=%d open=%d other=%d max=%d\n", ch,open,other,max);
  56.                         break;
  57.                 }
  58.         }

  59.         return EXIT_SUCCESS;
  60. }

复制代码

论坛徽章:
0
8 [报告]
发表于 2007-05-18 23:33 |只看该作者
晕啊。没人帮我看一下啊。代码很简单的。

说老实话,我是基础不好。可一时间又找不出错误。。。

哎。。。。。。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
9 [报告]
发表于 2007-05-19 00:12 |只看该作者
原帖由 hcq0411 于 2007-5-18 23:33 发表
晕啊。没人帮我看一下啊。代码很简单的。

说老实话,我是基础不好。可一时间又找不出错误。。。

哎。。。。。。

你的帖子/代码这么零乱,你让别人怎么看得下去呢?

唉……

论坛徽章:
0
10 [报告]
发表于 2007-05-19 09:55 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP