您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關C++怎么實現24點游戲,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體內容如下
隨機生成4個代表撲克牌牌面的數字字母,程序自動列出所有可能算出24的表達式,用擅長的語言(C/C++/Java或其他均可)實現程序解決問題。
程序風格良好(使用自定義注釋模板)
列出表達式無重復。
以下為源代碼
#include<iostream> #include<string> #include <stdlib.h> #include<time.h> using namespace std; char card[] = { 'A','2','3','4','5','6','7','8','9','10','J','Q','K' }; char buf[4]; double nums[4]; char ope[4] = { '+','-','*','/' }; void cre()//生成 { int i = 0; int j; cout << "生成的四張牌面為:"; srand((unsigned)time(0)); for (i = 0; i<4; i++) { j =rand() % 13; buf[i] = card[j]; } cout << buf[0] << ";" << buf[1] << ";" << buf[2] << ";" << buf[3] << "。" << endl; for (i = 0; i<4; i++) { if (buf[i] == 'A') nums[i] = 1; else if(buf[i] == '2') nums[i] = 2; else if (buf[i] == '3') nums[i] = 3; else if (buf[i] == '4') nums[i] = 4; else if (buf[i] == '5') nums[i] = 5; else if (buf[i] == '6') nums[i] = 6; else if (buf[i] == '7') nums[i] = 7; else if (buf[i] == '8') nums[i] = 8; else if (buf[i] == '9') nums[i] = 9; else if (buf[i] == '10') nums[i] = 10; else if (buf[i] == 'J') nums[i] = 11; else if (buf[i] == 'Q') nums[i] = 12; else if (buf[i] == 'K') nums[i] = 13; } } double calcute(double a, double b, char index) { if (index == '+') return a + b; //若為+,則返回相應結果 else if (index == '-') return a - b; else if (index == '*') return a*b; else if (index == '/') if (b != 0) return a / b; //只有當分母不為0時,返回結果 } void exh()//窮舉計算 { double temp[3], tem[2]; //第一個符號放置后,經過計算后相當于剩下三個數,這個數組用于存儲這三個數 double sum; //求得的和 int judge = 0; //判斷是否找到一個合理的解 for (int i = 0; i < 4; i++) //第一次放置的符號 { for (int j = 0; j < 4; j++) //第二次放置的符號 { for (int k = 0; k < 4; k++) //第三次放置的符號 { for (int m = 0; m < 3; m++) //首先計算的兩個相鄰數字,共有3種情況,相當于括號的作用 { if (nums[m + 1] == 0 && ope[i] == '/') break; temp[m] = calcute(nums[m], nums[m + 1], ope[i]); temp[(m + 1) % 3] = nums[(m + 2) % 4]; temp[(m + 2) % 3] = nums[(m + 3) % 4]; //先確定首先計算的兩個數字,計算完成相當于剩下三個數,按順序儲存在temp數組中 for (int n = 0; n < 2; n++) //三個數字選出先計算的兩個相鄰數字,兩種情況,相當于第二個括號 { if (temp[n + 1] == 0 && ope[j] == '/') break; tem[n] = calcute(temp[n], temp[n + 1], ope[j]); tem[(n + 1) % 2] = temp[(n + 2) % 3]; //先確定首先計算的兩個數字,計算完成相當于剩下兩個數,按順序儲存在temp數組中 if (tem[1] == 0 && ope[k] == '/') break; sum = calcute(tem[0], tem[1], ope[k]); //計算和 if (sum == 24) //若和為24 { judge = 1; //判斷符為1,表示已求得解 if (m == 0 && n == 0) cout << "((" << nums[0] << ope[i] << nums[1] << ")" << ope[j] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl; else if (m == 0 && n == 1) cout << "(" << nums[0] << ope[i] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[j] << nums[3] << ")=" << sum << endl; else if (m == 1 && n == 0) cout << "(" << nums[0] << ope[j] << "(" << nums[1] << ope[i] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl; else if (m == 1 && n == 1) cout << nums[0] << ope[k] << "((" << nums[1] << ope[i] << nums[2] << ")" << ope[j] << nums[3] << ")=" << sum << endl; else if (m == 2 && n == 0) cout << "(" << nums[0] << ope[j] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[i] << nums[3] << ")=" << sum << endl; else if (m == 2 && n == 0) cout << nums[0] << ope[k] << "(" << nums[1] << ope[j] << "(" << nums[2] << ope[i] << nums[3] << "))=" << sum << endl; //m=0,1,2 n=0,1表示六種括號放置可能,并按照這六種可能輸出相應的格式的計算式 } } } } } } if (judge == 0) cout << "這四張撲克牌無法找到一個合理的解" << endl; //如果沒有找到結果,符號位為0 } int main() { int i; int select = 1; cout<< " ################################################" << endl << " # #" << endl << " # 歡迎進入24點游戲 #" << endl << " # #" << endl << " ################################################" << endl; while (select) { cout<< " ################################################" << endl << " # #" << endl << " # 是否開始游戲 #" << endl << " # #" << endl << " # 0.是 1.否 #" << endl << " # #" << endl << " ################################################" << endl; cout << "請輸入你的選擇(0或1):"; cin >> i; switch (i) { case 0: cre(); exh(); break; case 1: select = 0; break; default: cout << "請在0和1之間選擇!" << endl; } } return 0; }
效果圖1
關于“C++怎么實現24點游戲”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。