抢30游戏 算法

本文分析了抢30游戏的必胜策略,揭示了游戏的不公平性,并通过逆推法和循环法详细解释了获胜的关键。同时,提供了C++代码实现,展示了如何通过编程模拟游戏过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

由两个人玩“抢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”的游戏是偏向先开口的人。

下面是完整的代码:

#include<time.h>
#include<iostream>
#include<string>
using namespace std;
//抢30
int noAI();
int inAI(int num);
int randNum(int rangeMin,int rangeMax)
{
 return static_cast<double>(rand())/(RAND_MAX+1)*(rangeMax-rangeMin+1)+rangeMin; 
}
int main()
{
 int sum = 0;
 int choice = 0;
 int first = 0;
 int AI = 0;
 cout << "抢30游戏,请选择谁先开始:\n1.Person\n2.PC" << endl;
 cin >> first;
 cout << "是否打开AI:\n1.Close AI\n2.Open AI" << endl;
 cin >> AI;
 while(sum <= 30)
 {
  srand(time(NULL));//noAI时随机种子
  if(first == 1)
  {//人类先
   if(AI == 1)
   {//没有AI
    cout << "Person choice 1 or 2?:" << endl;
    cin >> choice;
    if(choice >2)
    {
     cout << "耍阴招,Person lose!";
     break;
    }
    sum += choice;
    if(sum == 30)
    {
     cout << "Person win!" << endl;
     break;
    }
    cout << "现在点数:" << sum << endl;
    int pc = noAI();
    cout << "pc选择:" << pc << "点" << endl;
    sum += pc;
    if(sum >= 30)
    {
     cout << "AI win!" << endl;
     break;
    }
   }
   else
   {//有AI
    cout << "Person choice 1 or 2?:" << endl;
    cin >> choice;
    if(choice >2)
    {
     cout << "耍阴招,Person lose!";
     break;
    }
    sum += choice;
    if(sum == 30)
    {
     cout << "Person win!" << endl;
     break;
    }
    cout << "现在点数:" << sum << endl;
    int pc = inAI(sum);
    cout << "pc选择:" << pc << "点" << endl;
    sum += pc;
    if(sum >= 30)
    {
     cout << "AI win!" << endl;
     break;
    }
   }
  }
  else
  {//人类后
   if(AI == 1)
   {//没有AI
    int pc = noAI();
    sum += pc;
    if(sum >= 30)
    {
     cout << "AI win!" << endl;
     break;
    }
    cout << "PC选择:" << pc << "点" << endl;
    cout << "现在点数:" << sum << endl;
    cout << "Person choice 1 or 2?:" << endl;
    cin >> choice;
    if(choice >2)
    {
     cout << "耍阴招,Person lose!";
     break;
    }
    sum += choice;
    if(sum == 30)
    {
     cout << "Person win!" << endl;
     break;
    }
   }
   else
   {//有AI
    int pc = inAI(sum);
    sum += pc;
    if(sum >= 30)
    {
     cout << "AI win!" << endl;
     break;
    }
    cout << "PC选择:" << pc << "点" << endl;
    cout << "现在点数:" << sum << endl;
    cout << "Person choice 1 or 2?:" << endl;
    cin >> choice;
    if(choice >2)
    {
     cout << "耍阴招,Person lose!";
     break;
    }
    sum += choice;
    if(sum == 30)
    {
     cout << "Person win!" << endl;
     break;
    }
   }
  }
  cout << "现在点数:" << sum << endl;
 }
 return 0;
}
int noAI()
{
 return randNum(1,2);
}
int inAI(int num)
{
 int choice = 0;
 if(num == 0)
  return noAI();
 if(num%3 == 0)
 {
  return noAI();
 }
 else
 {
  while(num%3 != 0)
  {
   num++;
   choice++;
  }
  return choice;
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值