var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-333696-1']); _gaq.push(['_trackPageview']); _gaq.push(['_trackPageLoadTime']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();
  • 2007年04月13日

    用5,6,7,8,9五个数字利用加减乘除各一次算得结果为52

    分类:

    代码
    class Program
        {
            static void solve(ArrayList opnd, ArrayList oper, string formula)
            {
                if (opnd.Count == 1)
                {
                    double n = (double) opnd[0];

                    if (Math.Abs(n - 52) < 0.1)
                    {
                        Console.WriteLine("{0} {1}", formula, n);
                    }

                    return;
                }

                for (int i = 0; i < opnd.Count; i++)
                {
                    for (int j = i + 1; j < opnd.Count; j++)
                    {
                        double n = (double)opnd[i];

                        double m = (double) opnd[j];

                        for (int k = 0; k < oper.Count; k++)
                        {
                            ArrayList newOpnd = opnd.Clone() as ArrayList;

                            newOpnd.RemoveAt(i);
                            newOpnd.RemoveAt(j - 1);
                                               
                            int op = (int)oper[k];
                            double v = 0;

                            ArrayList newOper = oper.Clone() as ArrayList;

                            string f = formula;

                            switch (op)
                            {
                                case 1: v = n + m; newOper.Remove(1); f += n + "+" + m + " ";  break;
                                case 2: v = n * m; newOper.Remove(2); f += n + "*" + m + " "; break;
                                case 3: v = n - m; newOper.Remove(3); newOper.Remove(4); f += n + "-" + m + " "; break;
                                case 4: v = m - n; newOper.Remove(3); newOper.Remove(4); f += m + "-" + n + " "; break;
                                case 5: v = n / m; newOper.Remove(5); newOper.Remove(6); f += n + "/" + m + " "; break;
                                case 6: v = m / n; newOper.Remove(5); newOper.Remove(6); f += m + "/" + n + " "; break;
                            }

                            newOpnd.Add(v);

                            solve(newOpnd, newOper, f);
                        }
                    }
                }
            }

            static void Main(string[] args)
            {
                ArrayList opnd = new ArrayList();
                ArrayList oper = new ArrayList();

                opnd.Add(5.0);
                opnd.Add(6.0);
                opnd.Add(7.0);
                opnd.Add(8.0);
                opnd.Add(9.0);

                oper.Add(1);
                oper.Add(2);
                oper.Add(3);
                oper.Add(4);
                oper.Add(5);
                oper.Add(6);

                solve(opnd, oper, "");
            }
        }

    分享到:

    历史上的今天:

    VBScript 运算符 2011年04月13日