用C编程的一种改进的Nim游戏?
修改后的Nim游戏是一个优化数组游戏。此游戏根据起始玩家和最佳移动来预测获胜者。
游戏逻辑−在此游戏中,我们给定了一个数组{},其中包含元素。通常有两个玩家玩游戏,分别是player1和player2。双方的目标都是确保从数组中删除所有数字。现在,player1必须删除所有能被3整除的数字,而player2必须删除所有能被5整除的数字。目标是确保他们以最佳方式删除所有元素,并在此情况下找出获胜者。
示例
Array : {1,5, 75,2,65,7,25,6}
Winner : playerB.
A removes 75 -> B removes 5 -> A removes 6 -> B removes 65 -> No moves for A, B wins.代码预览
该代码会找到 A 可以移除的元素数、B 可以移除的元素数以及他们都可以移除的元素数。根据他们都可以移除的元素数,找到解决方案。由于 A 先移除元素,即使它必须比 B 移除多一个元素,它也可以赢得比赛。在正常情况下,拥有最多可移除元素数的玩家获胜。
求出 NIM 游戏解决方案的程序
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {1,5, 75,2,65,7,25,6};
int n = sizeof(arr) / sizeof(arr[0]);
int movesA = 0, movesB = 0, movesBoth = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 3 == 0 && arr[i] % 5 == 0)
movesBoth++;
else if (arr[i] % 3 == 0)
movesA++;
else if (arr[i] % 5 == 0)
movesB++;
}
if (movesBoth == 0) {
if (movesA > movesB)
cout<<"Player 1 is the Winner";
cout<<"Player 2 is the Winner";
}
if (movesA + 1 > movesB)
cout<<"Player 1 is the Winner";
cout<<"Player 2 is the Winner"; ;
return 0;
}输出
Player 2 is the Winner
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP