目錄
" >
Player 2 is the Winner
登入後複製
一個用C語言修改過的Nim遊戲?
遊戲程式設計
nim遊戲
c語言修改
Nim 的改進賽局是一個陣列的最佳化賽局。遊戲根據起始玩家和最佳移動來預測獲勝者。
#遊戲邏輯 - 在遊戲中,我們得到一個包含元素的陣列{}。通常有兩個玩家玩遊戲,即玩家 1 和玩家 2。兩者的目的都是確保從數組中刪除它們的所有數字。現在,玩家1必須刪除所有可被3整除的數字,玩家2必須刪除所有可被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 多刪除一個元素,它也可以獲勝。在正常情況下,刪除的元素數量最多的玩家獲勝。尋找遊戲解決方案的程序NIMArray : {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.
登入後複製
#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;
}
登入後複製
輸出#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
登入後複製
Player 2 is the Winner
登入後複製
以上是一個用C語言修改過的Nim遊戲?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)