“抢30”游戏

由两个人玩“抢30”游戏,游戏规则是:第一个人先说“1”或“2”,第二个人要接着往下说一个或两个数,然后又轮到第一个人,再接着往下说一个或两个数。这样两人反复轮流,每次每个人说一个或两个数都可以,但是不可以连说三个数,谁先抢到30,谁得胜。

问题分析

首先,分析这个游戏是否公平。一个游戏的公平性主要体现在游戏双方赢的机会性。

经分析可知,获胜者最后总能说到27,还有呢?获胜者陆续说出了 24,21,18,15,12,9,6,3。因此,只要能控制讲出上述数,就一定能在最后“抢到30”。在大家不知情的情况下,不管先说后说,都有赢的可能性,但游戏里潜藏着人为可控的必胜因素。还可以发现,失败者报1个数,获胜者就报2个数;失败者报2个数,获胜者就只报1个数。 所以获胜者总能迅速报数。

规律1使用逆推的方法。

要想抢到30,必须先抢到27,这样,无论对方说28或28、29,自己总能抢到30。要想抢到27,必须先抢到24,这样,无论对方说25或25、 26,自己总能抢到27……照此推理下去,要想抢到6,必须先要抢到3,这样无论对方说4或4、5,自己总能抢到6。最后,问题转化为如何抢到3,要想抢到3,只有让对方先开始,这样,无论对方先说1或1、2,自己总能抢到3。由此可见,这个游戏是偏向后开口的人,若这个人能抢到3,6,9,12,……,21,24, 27,则一定会赢,因此,这个游戏是不公平的。

规律2使用循环法。

根据游戏规则,第一个人可以在1或1、2中选择一个或两个数字,对于“抢30”游戏,第二个人总是可以控制每轮报数的个数为3,由于30可以被3整除,因此第二个人可以控制自己最后说到30从而获胜。

如果把“抢30”游戏改成“抢50”,可类似地进行分析。要先抢到50,就要先抢到 47,44,41,…,5, 2。因此,我们发现,“抢50”的游戏是偏向先开口的人。

下面是完整的代码:

    
    
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. int input(int t);
  5. int copu(int s);
  6. int main()
  7. {
  8. int tol=0;
  9. printf("\n* * * * * * * *catch thirty* * * * * * * \n");
  10. printf("Game Begin\n");
  11. rand(); /*初始化随机数发生器*/
  12. /*取随机数决定机器和人谁先走第一步。若为1,则表示人先走第一步*/
  13. if(rand()%2)
  14. tol=input(tol);
  15. while(tol!=30) /*游戏结束条件*/
  16. if((tol=copu(tol)) == 30) /*计算机取一个数,若为30则机器胜利*/
  17. printf("I lose! \n");
  18. else
  19. if((tol=input(tol)) == 30) /*人取一个数,若为30则人胜利*/
  20. printf("I lose! \n");
  21. printf(" * * * * * * * *Game Over * * * * * * * *\n");
  22. return 0;
  23. }
  24. int input(int t)
  25. {
  26. int a;
  27. do{
  28. printf("Please count:");
  29. scanf("%d", &a);
  30. if(a>2 || a<1 || t+a>30)
  31. printf("Error input,again!");
  32. else
  33. printf("You count:%d\n", t+a);
  34. }while(a>2 || a<1 || t+a>30);
  35. return t+a; /*返回当前已经取走的数的累加和*/
  36. }
  37. int copu(int s)
  38. {
  39. int c;
  40. printf("Computer count:");
  41. if((s+1)%3 == 0) /*若剩余的数的模为1,则取1*/
  42. printf(" %d\n",++s);
  43. else
  44. if((s+2)%3 == 0)
  45. {
  46. s+=2; /*若剩余的数的模为2,则取2*/
  47. printf(" %d\n",s);
  48. }
  49. else
  50. {
  51. c=rand()%2+1; /*否则随机取1或2*/
  52. s+=c;
  53. printf(" %d\n",s);
  54. }
  55. return s;
  56. }
下面是运行结果:
* * * * * * * *catch thirty* * * * * * *
Game Begin
Please count:1
You count:1
Computer count: 3
Please count:4
Error input,again!Please count:2
You count:5
Computer count: 6
Please count:1
You count:7
Computer count: 9
Please count:2
You count:11
Computer count: 12
Please count:2
You count:14
Computer count: 15
Please count:2
You count:17
Computer count: 18
Please count:
2
You count:20
Computer count: 21
Please count:2
You count:23
Computer count: 24
Please count:2
You count:26
Computer count: 27
Please count:2
You count:29
Computer count: 30
I lose!
* * * * * * * *Game Over * * * * * * * *
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值