# 北軟100 pA 推算撲克牌數字遊戲 ###### tags: `北軟100` ## 題目 說明:拿到 3 張撲克牌根據下列 4 個限制的條件推算撲克牌的數字 條件 1: 第 1 張牌 A 與第 2 張牌 B 的和 條件 2: 第 1 張牌 A 與第 3 張牌 C 的和 條件 3: 3 張牌的數字均小於多少 條件 4: 3 張牌的數字均不同 試設計一個程式, 可用來執行此推算撲克牌數字的遊戲。 評分項目: 1. 可以顯示類似圖一所示之操作畫面。其中條件 1~3 的值為使用者可以輸入的值, 另外, 3 張牌的背景圖請採用附件之 card.jpg 檔案。(5 分) 2. 可以正確的列出所有撲克牌的數字組合並顯示第一個解答之撲克牌數字(如圖二所示)。(15 分) 3. 當無解時, 可以在答案區顯示”無解” (如圖三所示)。(5 分) ![](https://i.imgur.com/3Gsfo2j.png) ![](https://i.imgur.com/rbEQG6m.png) ## 想法 這題我們可以用暴力解,開三個迴圈,三個迴圈變數分別代表撲克牌的點數。 接下來只需要寫上條件,如果符合條件再新增到List,並且記錄第一次湊成的點數。 ## 程式碼(C#) 請先將撲克牌新增至Resource。 [撲克牌圖片包](https://drive.google.com/file/d/1aX1OdxtWxx3yONKUv_2ieLJT_dGJJmp-/view?usp=sharing) purple_back.png是卡背材質。 ```csharp= using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace problemA { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Image[] card = { Properties.Resources.AC, Properties.Resources._2C, Properties.Resources._3C, Properties.Resources._4C, Properties.Resources._5C, Properties.Resources._6C, Properties.Resources._7C, Properties.Resources._8C, Properties.Resources._9C, Properties.Resources._10C, Properties.Resources.JC, Properties.Resources.QC, Properties.Resources.KC, }; private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); pictureBox1.Image = Properties.Resources.purple_back; pictureBox2.Image = Properties.Resources.purple_back; pictureBox3.Image = Properties.Resources.purple_back; int aplusb = Convert.ToInt16(textBox1.Text); int aplusc = Convert.ToInt16(textBox2.Text); int small = Convert.ToInt16(textBox3.Text); int a = 0, b = 0, c = 0; for(int i = 1; i <= 13; i++) { for(int j = 1; j <= 13; j++) { for(int k = 1; k <= 13; k++) { if (i == j || j == k || i == k) continue; if (i >= small || j >= small || k >= small) continue; if (i + j != aplusb || i + k != aplusc) continue; if(a == 0) { a = i; b = j; c = k; } listBox1.Items.Add("A=" + i + " B=" + j + " C=" + k); } } } if(a == 0) { listBox1.Items.Add("無解"); return; } pictureBox1.Image = card[a - 1]; pictureBox2.Image = card[b - 1]; pictureBox3.Image = card[c - 1]; } private void button2_Click(object sender, EventArgs e) { Environment.Exit(0); } } } ```