• 24 点实现代码


        class Program
        {
            static List<int> sort = new List<int>();
            static int[] intSort = new int[4];
            static int[] array = new int[] { 1, 3, 5, 9 };
            static bool[] arrayBool = new bool[4];//生成 记录表
            static void Main(string[] args)
            {
                for (int j = 0; j < 2; j++)
                {
                    random1();
                    //24点小游戏
                    //step1.生成数的全排
                    GenerateAllSort();
                    for (int i = 0; i < array.Length; i++)
                    {
                        Console.Write(array[i]);
                    }
                    Console.WriteLine();
                    Computer();
                }
                Console.Read();
            }
            private static void random1()
            {
                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = GenerateRandomInteger(1, 10);
                }
            }
            /// <summary>
            /// Returns an random interger number within a specified rage
            /// </summary>
            /// <param name="min">Minimum number</param>
            /// <param name="max">Maximum number</param>
            /// <returns>Result</returns>
            public static int GenerateRandomInteger(int min = 0, int max = int.MaxValue)
            {
                var randomNumberBuffer = new byte[10];
                new RNGCryptoServiceProvider().GetBytes(randomNumberBuffer);
                return new Random(BitConverter.ToInt32(randomNumberBuffer, 0)).Next(min, max);
            }
            /// <summary>
            /// 计算24点
            /// </summary>
            private static void Computer()
            {
                string[] comstr = new string[] { "+", "-", "*", "*" };
                int t = 4;
                for (int w = 0; w < sort.Count / 4; w++)
                {
                    int pp = w * 4;
                    int[] pp1 = new int[4];
                    sort.CopyTo(pp, pp1, 0, pp1.Length);
                    for (int i = 0; i < t; i++)
                    {
                        for (int j = 0; j < t; j++)
                        {
                            for (int q = 0; q < t; q++)
                            {
                                string one = comstr[i];
                                string tow = comstr[j];
                                string three = comstr[q];
                                if (CCC(comstr[q], CCC(comstr[j], CCC(comstr[i], pp1[0], pp1[1]), pp1[2]), pp1[3]) == 24)
                                {
                                    Console.Write("找到了:	");
                                    Console.WriteLine("((({0}{1}{2}){3}{4}){5}{6})=24", pp1[0], one, pp1[1], tow, pp1[2], three, pp1[3]);
                                }
                            }
                        }
                    }
                }
            }
            private static int CCC(string str, int first, int second)
            {
                int result = 0;
                switch (str)
                {
                    case "+": result = first + second; break;
                    case "-": result = first - second; break;
                    case "*": result = first * second; break;
                    case "/": result = first / second; break;
                }
                return result;
            }
            private static void GenerateAllSort(int step = 0)
            {
                if (step == 4)
                {
                    sort.AddRange(intSort);
                    return;
                }
                for (int i = 0; i < 4; i++)
                {
                    if (!arrayBool[i])
                    {
                        intSort[step] = array[i];
                        arrayBool[i] = true;
                        GenerateAllSort(step + 1);
                        arrayBool[i] = false;
                    }
                }
    
            }
        }
  • 相关阅读:
    2017年陕西省网络空间安全技术大赛WP
    XDCTF2014 Writeup
    TensorFlow入门测试程序
    python计算器
    CentOS安装crontab及使用方法
    在Linux 双机下自己手动实现浮动ip技术
    CentOS 7下安装配置FTP
    encodeURI 解码 编码
    jquery 消息提醒插件 toastmessage
    搭通自己的电脑与GitHub的传输通道
  • 原文地址:https://www.cnblogs.com/student-note/p/9014756.html
Copyright © 2020-2023  润新知