# 北軟99 pA 猜數字遊戲 ###### tags: `北軟99` ## 題目: 1. 請設計一程式, 可根據箭頭上所給定的數字(-20~20 之間的整數), 自動猜出在格子內的數字。例如:在下圖中給定 9, 5, 8, 4, 7, 6 等六個數字, 可猜出 a=1, b=4, c=5, d=3。 ![](https://i.imgur.com/xkcF8vW.png) 2. 提示:你可以利用 a + b = 5, a + c = 6, b + c = 9 這三個方程式先解出a, b, c 三個未知數, 再利用 b + d = 7 或其它包含 d 的方程式解出 d 的值。 3. 程式一開始執行時, 可顯示如圖一之畫面(必須畫線)。(3 分) 4. 當給定外圍的六個數字時, 按”開始”鍵, 能正確猜出在格子內的數字並顯示, 如圖二所示。(15 分) 5. 當無解時, 按”開始”鍵, 能顯示”無解”, 並將未知數回復到 a, b, c, d, 如圖三所示。(5 分) 6. 當輸入的數字超過範圍, 按”開始”鍵, 能顯示”數字超過範圍”, 如圖四所示。(2 分) ![](https://i.imgur.com/W558oJC.png) ## 想法: 這題是水題,由於提示已經很明顯的告訴你要怎麼做了,只需要按表施工就好。 處理$a+b,a+c,b+d,a+d,c+d,c+b$ 然後先$(a+b) + (b+c) + (a+c) = 2(a+b+c)$ 接下來除以二就能得到$(a+b+c)$ 依序列出 $c = (a+b+c) - (a+b)$ $b = (a+b+c) - (a+c)$ $a = (a+b+c) - (b+c)$ 最後 $d = (b+d) - b$ 依序print到對應的格子上即可。 ## 程式碼(C#) ```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(); } private void button1_Click(object sender, EventArgs e) { textBox1.Text = "a"; textBox2.Text = "b"; textBox3.Text = "c"; textBox4.Text = "d"; label1.Text = ""; int aplusb = Convert.ToInt16(textBox6.Text); //a+b的那一格 int aplusc = Convert.ToInt16(textBox10.Text); //a+c的那一格 int bplusd = Convert.ToInt16(textBox9.Text); //b+d的那一格 int aplusd = Convert.ToInt16(textBox8.Text); //a+d的那一格 int cplusd = Convert.ToInt16(textBox7.Text); //c+d的那一格 int cplusb = Convert.ToInt16(textBox5.Text); //c+b的那一格 int two_aplusbplusc = (aplusb + cplusb + aplusc); int[] check_array = { aplusb, aplusc, bplusd, aplusd, cplusd, cplusb }; bool find = false; for(int i = 0; i < check_array.Length; i++) { if(check_array[i] > 20 || check_array[i] < -20) { find = true; break; } } if(find == true) { label1.Text = "數字超過範圍"; return; } if(two_aplusbplusc % 2 != 0) { label1.Text = "無解"; return; } int aplusbplusc = two_aplusbplusc / 2; int a = aplusbplusc - cplusb; int b = aplusbplusc - aplusc; int c = aplusbplusc - aplusb; int d = bplusd - b; textBox1.Text = a + ""; textBox2.Text = b + ""; textBox3.Text = c + ""; textBox4.Text = d + ""; } private void button2_Click(object sender, EventArgs e) { Environment.Exit(0); } private void Form1_Load(object sender, EventArgs e) { } } } ```