如题。把参与运算的数分三组:
1:取一个数--{8,3}
2:取两个相同的数--{0,1,16,64,6,9}
3:取两个不同的数--{11,5,24,8/3,3/8}然后对1、2和1、3分组运算比较,得到要找的。但这样,感觉具体实施算法的时候,还是挺困难的,请大家指点。提供具体思路也行

解决方案 »

  1.   

    4个数+-×÷()排列组合就可以了。
    以前写过一个简单的玩
    http://www.cnblogs.com/xiaoq/archive/2006/10/24/538636.html
      

  2.   

    楼上的大虾,你写的好壮观啊,我没敢往下看,而且还引用了microsoft.vsa.dll和microsoft.jscript.dll,介都是什么
      

  3.   

    这个最多也就是 ≈ 嘛,让计算机算,用整型表示也就挂了得 8 / (3 - 8.0 / 3)都想得太复杂了 ....
    ======================================
    用带精度的也是不行的。需要舍入.../*****************************************************************************
     * 24点计算程序
     * 本程序包含带括号的情况;程序中无解的情况即遍历所有组合,总共有3264种组合方式。
     * Created by Hunts.C
     * Blog: Http://hunts.cnblog.com/
     * Email: hunts.c#hotmail.com
     * Last Edited: 2007/08/19
    *****************************************************************************/
    using System;
    using System.Collections.Generic;
    using System.Text;namespace Cal24
    {
        /// <summary>
        /// 24点计算程序
        /// </summary>
        class Program
        {
            static void Main(string[] args)
            {
                thread();
            }        private static char[] symbols = new char[] { '+', '-', '*', '/' };
            private static float[] nums = new float[4];        private static void thread()
            {
                for (int i = 0; i < 4; i++)
                {
                    string s;
                    int result;
                    // 如果读取的不是数字,则重新读取输入
                    do
                    {
                        s = Console.ReadLine();
                    }
                    while(!Int32.TryParse(s, out result));
                    // 填充用于计算的数字数组
                    nums[i] = result;
                }
                Console.WriteLine("遍历了" + Calculate() + "种情况");
                Console.WriteLine("-------------------------");
                thread();
            }        /// <summary>
            /// 遍历计算24点。
            /// </summary>
            /// <returns>返回遍历过的组合数</returns>
            private static int Calculate()
            {
                float temp=0;
                int count=0;
                for (int a = 0; a < 4; a++)
                    for (int b = 0; b < 4; b++)
                    {
                        if (a == b)
                            continue;
                        for (int i = 0; i < 4; i++)
                        {
                            // 先取两个数进行四则运算
                            switch (i)
                            {
                                case 0:
                                    temp = nums[a] + nums[b];
                                    break;
                                case 1:
                                    temp = nums[a] - nums[b];
                                    break;
                                case 2:
                                    temp = nums[a] * nums[b];
                                    break;
                                case 3:
                                    temp = nums[a] / nums[b];
                                    break;
                            }
      

  4.   

    // 不使用括号,不可能组合出如:(7+5)*(1+1) 这样的结构
                            // 这种情况发生在第二个算数符号是'*'或'/',而第三个算数符号是'+'或'-'
                            for (int x = 0; x < 4; x++)
                            {
                                if (x == a || x == b)
                                    continue;
                                for (int y = 0; y < 4; y++)
                                {
                                    if (y == a || y == b || y == x)
                                        continue;
                                    for (int z = 0; z < 4; z++)
                                    {
                                        // 有八种组合情况                                    // 如果前面是加减运算
                                        if (i < 2)
                                        {
                                            switch (z)
                                            {
                                                case 0:
                                                    count++;
                                                    if (Math.Round(temp * (nums[x] + nums[y]), 4) == 24)
                                                    {
                                                        // 22是只是个Flag,可以是随便的一个int型数字,标志这是()()计算情形
                                                        WriteFormula(a, i, b, 2, x, 0, y, 22);
                                                        return count;
                                                    }
                                                    break;
                                                case 1:
                                                    count++;
                                                    if (Math.Round(temp * (nums[x] - nums[y]), 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, 2, x, 1, y, 22);
                                                        return count;
                                                    }
                                                    break;
                                                case 2:
                                                    count++;
                                                    if (Math.Round(temp / (nums[x] + nums[y]), 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, 3, x, 0, y, 22);
                                                        return count;
                                                    }
                                                    break;
                                                case 3:
                                                    count++;
                                                    if (Math.Round(temp / (nums[x] - nums[y]), 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, 3, x, 1, y, 22);
                                                        return count;
                                                    }
                                                    break;
                                            }
                                        }
                                        // 如果前面是乘除运算
                                        else
                                        {
                                            switch (z)
                                            {
                                                case 0:
                                                    count++;
                                                    if (Math.Round(temp + (nums[x] * nums[y]), 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, 0, z, 2, y, 22);
                                                        return count;
                                                    }
                                                    break;
                                                case 1:
                                                    count++;
                                                    if (Math.Round(temp + (nums[x] / nums[y]), 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, 0, z, 3, y, 22);
                                                        return count;
                                                    }
                                                    break;
                                                case 2:
                                                    count++;
                                                    if (Math.Round(temp - (nums[x] * nums[y]), 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, 1, z, 2, y, 22);
                                                        return count;
                                                    }
                                                    break;
                                                case 3:
                                                    count++;
                                                    if (Math.Round(temp - (nums[x] / nums[y]), 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, 1, z, 3, y, 22);
                                                        return count;
                                                    }
                                                    break;
                                            }
                                        }                                }
                                }
                            }
      

  5.   

    for (int c = 0; c < 4; c++)
                            {
                                // 排除已经使用的数字
                                if (c == a || c == b)
                                    continue;
                                float temp2 = temp;
                                for (int j = 0; j < 6; j++)
                                {
                                    // 将前面计算得到的值与第三个数进行四则运算
                                    switch (j)
                                    {
                                        case 0:
                                            temp2 = temp + nums[c];
                                            break;
                                        case 1:
                                            temp2 = temp - nums[c];
                                            break;
                                        case 2:
                                            temp2 = temp * nums[c];
                                            break;
                                        case 3:
                                            temp2 = temp / nums[c];
                                            break;
                                        // 不使用括号,不可能组合出如:(5-1/5)*5 这样的结构
                                        // 所以必须有第三个数减去前面两个数的计算结果的情况
                                        case 4:
                                            temp2 = nums[c] - temp;
                                            break;
                                    }
                                    for (int d = 0; d < 4; d++)
                                    {
                                        // 排除已经使用的数字
                                        if (d == a || d == b || d == c)
                                            continue;
                                        for (int k = 0; k < 5; k++)
                                        {
                                            switch (k)
                                            {
                                                case 0:
                                                    count++;
                                                    // 由于计算机计算 (2/11)*11 != 2 即使是单精度或双精度,因此需要舍入
                                                    if (Math.Round(temp2 + nums[d], 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, j, c, k, d);
                                                        return count;
                                                    }
                                                    break;
                                                case 1:
                                                    count++;
                                                    if (Math.Round(temp2 - nums[d], 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, j, c, k, d);
                                                        return count;
                                                    }
                                                    break;
                                                case 2:
                                                    count++;
                                                    if (Math.Round(temp2 * nums[d], 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, j, c, k, d);
                                                        return count;
                                                    }
                                                    break;
                                                case 3:
                                                    count++;
                                                    if (Math.Round(temp2 / nums[d], 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, j, c, k, d);
                                                        return count;
                                                    }
                                                    break;
                                                // 不使用括号,不可能组合出如:6/(1-3/4) 这样的结构
                                                // 所以必须有第四个数除以前面三个数的计算结果的情况
                                                case 4:
                                                    count++;
                                                    if (Math.Round(nums[d] / temp2, 4) == 24)
                                                    {
                                                        WriteFormula(a, i, b, j, c, k, d);
                                                        return count;
                                                    }
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                Console.WriteLine("无解!");
                return count;
            }        /// <summary>
            /// 构造24点表达式
            /// </summary>
            /// <param name="s">7个参数的索引</param>
            private static void WriteFormula(params int[] s)
            {
                // 考虑使用StringBuilder?            string str1, str2, str3;
                str1 = nums[s[0]].ToString() + symbols[s[1]] + nums[s[2]].ToString();
                // 带有第8个参数的情况
                if (s.Length == 8)
                {
                    str3 = "(" + str1 + ")" + symbols[s[3]] + "(" + nums[s[4]].ToString() + symbols[s[5]] + nums[s[6]].ToString() + ")";
                }
                else
                {
                    // 如果是第三个数减去前面两个数的计算结果
                    if (s[3] == 4)
                    {
                        str2 = nums[s[4]].ToString() + "-(" + str1 + ")";
                    }
                    else
                    {
                        str2 = "(" + str1 + ")" + symbols[s[3]] + nums[s[4]].ToString();
                    }
                    // 如果是第四个数除以前面三个数的计算结果
                    if (s[5] == 4)
                    {
                        str3 = nums[s[6]].ToString() + "/(" + str2 + ")";
                    }
                    else
                    {
                        str3 = "(" + str2 + ")" + symbols[s[5]] + nums[s[6]].ToString();
                    }
                }
                Console.WriteLine(str3 + "=24");
            }
        }
    }