C#计算24点(1-13数字)

Node.cs

namespace count24
{
    class Node
    {
        public Node(double val, String e)
        {
            value = val;
            exp = e;
        }
        public Node()
        {

        }
        public double value;
        public String exp;
    }
}

Program.cs

namespace count24
{
    internal class Program
    {
        static void Main(string[] args)
        {
        wait_input:
            Console.Write("A:");
            string a = Console.ReadLine();
            Console.Write("B:");
            string b = Console.ReadLine();
            Console.Write("C:");
            string c = Console.ReadLine();
            Console.Write("D:");
            string d = Console.ReadLine();

            List<Node> list = new List<Node>();
            list.Add(new Node(Convert.ToDouble(a), a));
            list.Add(new Node(Convert.ToDouble(b), b));
            list.Add(new Node(Convert.ToDouble(c), c));
            list.Add(new Node(Convert.ToDouble(d), d));

            Console.WriteLine("count result:");
            countProcess(list);

            goto wait_input;
        }

        static void countProcess(List<Node> list)
        {
            if (list.Count < 2)
                return;
            if (list.Count == 2)
            {
                if (list[0].value + list[1].value == 24)
                {
                    Console.WriteLine(list[0].exp + "+" + list[1].exp);
                }
                if (list[0].value * list[1].value == 24)
                {
                    Console.WriteLine(list[0].exp + "*" + list[1].exp);
                }
                if (list[0].value / list[1].value == 24)
                {
                    Console.WriteLine(list[0].exp + "/" + list[1].exp);
                }
                if (list[1].value / list[0].value == 24)
                {
                    Console.WriteLine(list[1].exp + "/" + list[0].exp);
                }
                if (list[0].value - list[1].value == 24)
                {
                    Console.WriteLine(list[0].exp + "-" + list[1].exp);
                }
                if (list[1].value - list[0].value == 24)
                {
                    Console.WriteLine(list[1].exp + "-" + list[0].exp);
                }
                return;
            }
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = i + 1; j < list.Count; j++)
                {
                    List<Node> clist = new List<Node>(list);
                    //clist.CopyTo()
                    //clist = list;
                    //list.CopyTo(clist);
                    clist.Remove(list[i]);
                    clist.Remove(list[j]);

                    double a = list[i].value + list[j].value;
                    Node aa = new Node();
                    aa.value = a;
                    aa.exp = "(" + list[i].exp + "+" + list[j].exp + ")";
                    clist.Add(aa);
                    countProcess(clist);
                    clist.Remove(aa);

                    double b = list[i].value * list[j].value;
                    Node bb = new Node();
                    bb.value = b;
                    bb.exp = "(" + list[i].exp + "*" + list[j].exp + ")";
                    clist.Add(bb);
                    countProcess(clist);
                    clist.Remove(bb);

                    double c = list[i].value - list[j].value;
                    Node cc = new Node();
                    cc.value = c;
                    cc.exp = "(" + list[i].exp + "-" + list[j].exp + ")";
                    clist.Add(cc);
                    countProcess(clist);
                    clist.Remove(cc);

                    double d = list[j].value - list[i].value;
                    Node dd = new Node();
                    dd.value = d;
                    dd.exp = "(" + list[j].exp + "-" + list[i].exp + ")";
                    clist.Add(dd);
                    countProcess(clist);
                    clist.Remove(dd);

                    if (list[j].value != 0)
                    {
                        double e = list[i].value / list[j].value;
                        Node ee = new Node();
                        ee.value = e;
                        ee.exp = "(" + list[i].exp + "/" + list[j].exp + ")";
                        clist.Add(ee);
                        countProcess(clist);
                        clist.Remove(ee);
                    }

                    if (list[i].value != 0)
                    {
                        double f = list[j].value / list[i].value;
                        Node ff = new Node();
                        ff.value = f;
                        ff.exp = "(" + list[j].exp + "/" + list[i].exp + ")";
                        clist.Add(ff);
                        countProcess(clist);
                        clist.Remove(ff);
                    }
                }
            }
        }
    }
}

 

posted @ 2023-08-23 11:23  威流  阅读(82)  评论(0编辑  收藏  举报