解猜数字(XAXB)

我的想法是:把所有备选答案当做正确答案和猜的数字对比,如果得出XAXB和给出的XAXB相同则保留

代码

        int a = ;
int b = ;
List<string> number = new List<string>(); private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text == "")
{
return;
}
if (button1.Text == "重置")
{
init();
return;
}
try
{
string[] state = textBox2.Text.Split('.');
a = Int32.Parse(state[]);
b = Int32.Parse(state[]);
if (a + b > || a < || b < )
{
return;
}
if (a == )
{
button1.Text = "重置";
return;
} for (int i = number.Count - ; i >= ; i--)
{
if (check(number[i], number[], a, b))
{
continue;
}
number.RemoveAt(i);
} richTextBox1.Text = "";
for (int i = ; i < number.Count; i++)
{
richTextBox1.Text += number[i] + "\n";
}
textBox2.Text = "";
textBox1.Text = number[];
}
catch
{
return;
}
} private void Form1_Load(object sender, EventArgs e)
{
init();
} private void init()
{
a = ;
b = ;
button1.Text = "确定";
number.Clear();
textBox1.Text = "";
textBox2.Text = "";
richTextBox1.Text = "";
for (int aa = ; aa < ; aa++)
{
for (int bb = ; bb < ; bb++)
{
if (bb == aa) continue;
for (int cc = ; cc < ; cc++)
{
if (cc == aa || cc == bb) continue;
for (int dd = ; dd < ; dd++)
{
if (dd == aa || dd == bb || dd == cc) continue;
number.Add(aa + "," + bb + "," + cc + "," + dd);
}
}
}
}
textBox1.Text = number[];
} private bool check(string num, string guess, int a, int b)
{
int a1 = ;
int b1 = ;
string[] nums = num.Split(',');
string[] guesss = guess.Split(',');
for (int i = ; i < nums.Length; i++)
{
for (int j = ; j < guesss.Length; j++)
{
if (nums[i] == guesss[j])
{
if (i == j)
{
a1++;
}
else
{
b1++;
}
}
}
}
if (a == a1 && b == b1)
{
return true;
}
else
{
return false;
}
} private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
button1.PerformClick();
}
}

展现页面

解猜数字(XAXB)

上一篇:三维几何中的旋转表示:四元数、欧拉角和旋转矩阵


下一篇:Android开发的初学者快速创建一个项目