解猜数字(XAXB)

Posted margin_gu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解猜数字(XAXB)相关的知识,希望对你有一定的参考价值。

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

 

代码

        int a = 0;
        int b = 0;
        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[0]);
                b = Int32.Parse(state[1]);
                if (a + b > 4 || a < 0 || b < 0)
                {
                    return;
                }
                if (a == 4)
                {
                    button1.Text = "重置";
                    return;
                }

                for (int i = number.Count - 1; i >= 0; i--)
                {
                    if (check(number[i], number[0], a, b))
                    {
                        continue;
                    }
                    number.RemoveAt(i);
                }

                richTextBox1.Text = "";
                for (int i = 0; i < number.Count; i++)
                {
                    richTextBox1.Text += number[i] + "\\n";
                }
                textBox2.Text = "";
                textBox1.Text = number[0];
            }
            catch
            {
                return;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            init();
        }

        private void init()
        {
            a = 0;
            b = 0;
            button1.Text = "确定";
            number.Clear();
            textBox1.Text = "";
            textBox2.Text = "";
            richTextBox1.Text = "";
            for (int aa = 0; aa < 10; aa++)
            {
                for (int bb = 0; bb < 10; bb++)
                {
                    if (bb == aa) continue;
                    for (int cc = 0; cc < 10; cc++)
                    {
                        if (cc == aa || cc == bb) continue;
                        for (int dd = 0; dd < 10; dd++)
                        {
                            if (dd == aa || dd == bb || dd == cc) continue;
                            number.Add(aa + "," + bb + "," + cc + "," + dd);
                        }
                    }
                }
            }
            textBox1.Text = number[0];
        }

        private bool check(string num, string guess, int a, int b)
        {
            int a1 = 0;
            int b1 = 0;
            string[] nums = num.Split(\',\');
            string[] guesss = guess.Split(\',\');
            for (int i = 0; i < nums.Length; i++)
            {
                for (int j = 0; 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();
            }
        }
View Code

 

展现页面

以上是关于解猜数字(XAXB)的主要内容,如果未能解决你的问题,请参考以下文章

10个JavaScript代码片段,使你更加容易前端开发。

golang代码片段(摘抄)

JavaScript - 代码片段,Snippets,Gist

JavaScript笔试题(js高级代码片段)

手写数字识别——基于全连接层和MNIST数据集

LeetCode810. 黑板异或游戏/455. 分发饼干/剑指Offer 53 - I. 在排序数组中查找数字 I/53 - II. 0~n-1中缺失的数字/54. 二叉搜索树的第k大节点(代码片段