首页 > 编程语言 >C#计算24点(1-13数字)

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

时间:2023-08-23 11:34:25浏览次数:32  
标签:24 Node 13 Console C# list value clist exp

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);
                    }
                }
            }
        }
    }
}

 

标签:24,Node,13,Console,C#,list,value,clist,exp
From: https://www.cnblogs.com/valu/p/17650752.html

相关文章

  • 2、oracle迁移到postgres-事务提交方式不同
    目录oracle迁移到postgres-事务提交方式不同1、oracle2、postgresoracle迁移到postgres-事务提交方式不同1、oracleoracle默认不自动提交事务,需要手动commit2、postgrespostgres默认自动提交事务,不需要手动commit,导致迁移过来的pck如果有commit需要注释掉......
  • 1、oracle迁移到postgres-执行sql方式execute不同
    目录oracle迁移到postgres-执行sql方式execute不同1、oracle使用executeimmediate2、postgres使用executeoracle迁移到postgres-执行sql方式execute不同1、oracle使用executeimmediateexecuteimmediate'sql';2、postgres使用executeexecuteimmediate'sql';......
  • 5、oracle迁移到postgres-oracle中使用的`nvl`函数更改为统一的`coalesce`函数
    目录oracle迁移到postgres-oracle中使用的nvl函数更改为统一的coalesce函数1、oracle的nvl函数2、postgre的coalesce函数oracle迁移到postgres-oracle中使用的nvl函数更改为统一的coalesce函数nvl函数与coalesce函数都是值非空时,给默认值,oracle中也存在coalesce函数1、oracle的......
  • CO2激光器
    二氧化碳激光器,可称“隐身人”,CO2激光器是一种分子激光,主要的物质是二氧化碳分子,于1964年首次应用,其激光波长为10.6um,“身”处红外区,肉眼不能觉察,它的工作方式有连续、脉冲两种,用于激光切割,焊接,钻孔和表面处理。连续方式产生的激光功率可达20千瓦以上。脉冲方式产生波长10.6微......
  • 4、oracle迁移到postgres-oracle中使用的`decode`函数使用`case when`统一语法
    目录oracle迁移到postgres-oracle中使用的decode函数使用casewhen统一语法1、oracle的decode语法2、postgres的casewhenoracle迁移到postgres-oracle中使用的decode函数使用casewhen统一语法oracle中也有使用casewhen语法,使用decode函数比较简洁。1、oracle的decode语法匹......
  • 安装Nacos
    安装Nacos1.去github上下载nacos压缩包下载地址https://github.com/alibaba/nacos2.解压,解压文件中,bin是启动脚本,conf是配置文件3.配置文件:如果8488端口被占用要去conf中application.properties文件去修改还要配置数据库连接db.num=1db.url.0=jdbc:mysql://127.0.0.1:3306/r......
  • docker部署MySQL、tomcat
    一、安装MySQL1、dockerpullmysql:latest拉取镜像dockersearchmysql命令将返回与关键字“mysql”相关的Docker镜像列表。在结果中,你将看到镜像名称、描述、星级评分、官方/认证标志以及其他相关信息。结果包括了许多不同的MySQL镜像,包括官方支持的和由社区维护的镜像。你......
  • 在Mac系统上构建适用于Linux 64位的Go程序
    要在Mac系统上构建适用于Linux64位的Go程序,可以采用以下2种方式:1.通过设置环境变量并使用交叉编译来实现以下是在Mac系统上构建适用于Linux64位的Go程序的步骤:在你的项目根目录下,打开终端。设置环境变量GOOS和GOARCH为linux和amd64,分别表示目标操作系统为Linux,目......
  • Pybind11:使用C++编写Python模块
    放假摆了一周了。看论文实在不是什么有意思的活。这两天研究了一下Pybind11的用法。使用C/C++和Python混合编程的想法很早就有了,在大一的一次比赛时曾经实践过(虽然不是我写的),当时获得了比较显著的性能提升。但是当时用的是Swig,据队友说Swig对于NumPy的支持极为阴间,当时调试花了好......
  • 「题解」Codeforces 1063F String Journey
    先reverse一下。不难看出选出的字符串长度为\(1,2,\cdots,k\)一定不劣,仅考虑这种形式的。然后考虑一手dp,设\(f_{i}\)表示最后一个子串是\(i\)为结尾,最长长度是多少。这样转移就是\(f_i\getsf_{j}+1,iff\s[j-f_j+1,j]\text{is}s[i-f_j,i]\text{'ssubstring}\)......