C#计算24点

#region   24点算法
        /*
         * Count24(3,3,7,7)
         * 穷举法
         *
         */

        private string[] countMethod = new string[] { "+", "-", "*", "/" };
        private int[] countNum;
        private int[] countNumBak;

        public string Count24(int a, int b, int c, int d)
        {
            countNumBak = new int[4] { a, b, c, d };
            countNum = new int[4];
            string result = "没有找到合适的方法";
            bool isTrue;

            //把   abcd   四个数字随机付给数组   countNum
            for (int i = 0; i < 4; i++)
            {
                countNum[0] = countNumBak[i];
                for (int j = 0; j < 4; j++)
                {
                    if (j == i)
                        continue;
                    countNum[1] = countNumBak[j];
                    for (int k = 0; k < 4; k++)
                    {
                        if (k == j || k == i)
                            continue;
                        countNum[2] = countNumBak[k];
                        countNum[3] = countNumBak[1 + 2 + 3 - i - j - k];

                        result = countMain24(countNum, out isTrue);
                        if (!isTrue)
                        {
                            result = countMain(countNum, out isTrue);
                        }

                        if (isTrue)
                            return result;
                        else
                            result = "没有找到合适的方法";
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 组合计算,(第一个数字(方法)第二个数字)  (方法)  (第三个数字)(方法)(第四个数字)
        /// </summary>
        /// <param name="countNum"></param>
        /// <param name="isTrue"></param>
        /// <returns></returns>
        private string countMain(int[] countNum, out bool isTrue)
        {
            float a, b, c;
            string result = string.Empty;
            isTrue = false;
           
            foreach (string method in countMethod)
            {
                a = eval(method, (float)countNum[0], (float)countNum[1]);
                foreach (string m in countMethod)
                {
                    b = eval(method, (float)countNum[2], (float)countNum[3]);

                    foreach (string n in countMethod)
                    {
                        c = eval(n, a, b);

                        if (Math.Round(c, 4) == 24)
                        {
                            result = "(" + countNum[0].ToString() + method + countNum[1].ToString() + ")";
                            result += n + "("+countNum[2].ToString() + m + countNum[3].ToString() +")";
                            isTrue = true;
                            goto TODO;
                        }
                    }

                }
            }
            TODO:
            return result;
        }

        /// <summary>
        /// 顺序计算,第一个数字(方法)第二个数字(方法)第三个数字(方法)第四个数字
        /// </summary>
        /// <param name="countNum"></param>
        /// <param name="isTrue"></param>
        /// <returns></returns>
        private string countMain24(int[] countNum, out bool isTrue)
        {
            string result = "";
            float countValue = 0;
            float countValueBak = 0;
            isTrue = false;
            float upValueA, upValueBakA;
            float upValueB, upValueBakB;

            //   a   (方法)   b   (方法)   c   (方法)   d
            for (int i = 0; i < 4; i++)
            {   //不必计算   b/a   的情况,数组重排列中会计算到此种情况
                if (countMethod[i] == "/" && countNum[1] == 0)
                    countValue = (float)countNum[0];
                else
                    countValue = eval(countMethod[i], (float)countNum[0], (float)countNum[1]);

                upValueA = countValue;
                upValueBakA = countValue;

                for (int j = 0; j < 4; j++)
                {
                    //第一种情况   (a和b的结果)   (方法)   c    
                    if (countMethod[j] == "/" && countNum[2] == 0)
                    { }
                    else
                    {
                        countValue = eval(countMethod[j], upValueA, (float)countNum[2]);
                    }

                    //第二种情况   c   (方法)   (a和b的结果)
                    if (countMethod[j] == "/" && upValueBakA == 0)
                    {
                        countValueBak = upValueBakA;
                    }
                    else
                    {
                        countValueBak = eval(countMethod[j], (float)countNum[2], upValueBakA);
                    }

                    upValueB = countValue;
                    upValueBakB = countValueBak;

                    for (int k = 0; k < 4; k++)
                    {
                        //第一种情况   d   (方法)   (a,b,c的结果1)
                        if (countMethod[k] == "/" && upValueB == 0)
                        { }
                        else
                        {
                            countValue = eval(countMethod[k], (float)countNum[3], upValueB);
                            if (Math.Round(countValue, 4) == 24)
                            {//如果已经得到24点,则结束本程序
                                result = countNum[3].ToString() + countMethod[k] + "((" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + ")";
                                result += countMethod[j] + countNum[2].ToString() + ")";
                                result += "   =   24";
                                isTrue = true;
                                return result;
                            }
                        }

                        //第二种情况   (a,b,c的结果1)   (方法)   d
                        if (countMethod[k] == "/" && countNum[3] == 0)
                        { }
                        else
                        {
                            countValue = eval(countMethod[k], upValueB, (float)countNum[3]);
                            if (Math.Round(countValue, 4) == 24)
                            {//如果已经得到24点,则结束本程序
                                result = "((" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + ")";
                                result += countMethod[j] + countNum[2].ToString() + ")";
                                result += countMethod[k] + countNum[3].ToString() + "   =   24";
                                isTrue = true;
                                return result;
                            }
                        }

                        //第三种情况   d   (方法)   (a,b,c的结果2)
                        if (countMethod[k] == "/" && upValueBakB == 0)
                        { }
                        else
                        {
                            countValueBak = eval(countMethod[k], (float)countNum[3], upValueBakB);
                            if (Math.Round(countValueBak, 4) == 24)
                            {//如果已经得到24点,则结束本程序
                                result = countNum[3].ToString() + countMethod[k] + "(" + countNum[2].ToString() + countMethod[j] + "(" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + "))";
                                result += "   =   24";
                                isTrue = true;
                                return result;
                            }
                        }

                        //第四种情况   (a,b,c的结果2)   (方法)   d
                        if (countMethod[k] == "/" && countNum[3] == 0)
                        { }
                        else
                        {
                            countValueBak = eval(countMethod[k], upValueBakB, (float)countNum[3]);
                            if (Math.Round(countValueBak, 4) == 24)
                            {//如果已经得到24点,则结束本程序
                                result = "(" + countNum[2].ToString() + countMethod[j] + "(" + countNum[0].ToString() + countMethod[i] + countNum[1].ToString() + "))";
                                result += countMethod[k] + countNum[3].ToString();
                                result += "   =   24";
                                isTrue = true;
                                return result;
                            }
                        }
                    }
                }
            }

            return "";
        }

        private float eval(string method, float a, float b)
        {
            switch (method)
            {
                case "+":
                    return a + b;
                case "-":
                    return a - b;
                case "*":
                    return a * b;
                case "/":
                    if (b == 0)
                    {
                        return a;
                    }
                    else
                    {
                        return a / b;
                    }
                default:
                    return 0;
            }
        }
        #endregion

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值