CountDown Numbers游戏评估方法太慢

帕夫

知名会员
已加入
2016年10月4日
留言内容
47
编程经验
1-3
我在c#中创建了CountDown Numbers游戏,它可以正常工作,但是在使用Stack来计算结果的评估方法中它太慢了。我尝试使用LinkedList而不是Stack,但是它甚至更慢,并且我也尝试使用Array,但是与Stack一样。有人可以帮我创建更快的计算方法吗?

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp19
{
    class Program
    {
        private static List<string> generateCombinations(string combination, List<string> input, int pos, int length,
            int inputCount = 0, List<string> combinations = null)
        {
            if (combinations == null)
            {
                combinations = new List<string>();
            }

            if (pos < length)
            {
                foreach (string num in input)
                {
                    generateCombinations(combination + num, input, pos + 1, length, inputCount, combinations);
                }
            }
            else
            {
                if (!combination.Contains("+") && !combination.Contains("-") && !combination.Contains("*") && !combination.Contains("/"))
                {
                    if (!input.Contains("2"))
                    {
                        if (combination.Count(s => s == '0') == inputCount && isSelectorValid(combination))
                        {
                            combinations.Add(combination);
                        }
                    }
                    else if (checkForDuplicate(combination))
                    {
                        combinations.Add(combination);
                    }
                }
                else
                {
                    combinations.Add(combination);
                }
            }

            return combinations;
        }

        private static bool checkForDuplicate(string combination)
        {
            return combination.Distinct().Count() == combination.Length;
        }

        private static bool isSelectorValid(string selectorsCombination)
        {
            int numValues = 0;

            for (int i = 0; i < selectorsCombination.Length; i++)
            {
                if (selectorsCombination[i].ToString().Equals("1"))
                {
                    if (--numValues <= 0)
                    {
                        return false;
                    }
                }
                else
                {
                    ++numValues;
                }
            }

            return numValues == 1;
        }

        private static int evaluate(string selectors, List<string> nums, string operators,
            int numPos, int operatorPos)
        {
            Stack<int> stack = new Stack<int>();
            int res = 0;

            for (int i = 0; i < selectors.Length; i++)
            {
                if (selectors[i] == '0')
                {
                    stack.Push(Convert.ToInt32(nums[numPos++]));
                    continue;
                }

                int top = stack.Peek();
                stack.Pop();

                switch (operators[operatorPos++])
                {
                    case '+':
                        stack.Push(stack.Pop() + top);
                        break;
                    case '-':
                        stack.Push(stack.Pop() - top);
                        break;
                    case '*':
                        stack.Push(stack.Pop() * top);
                        break;
                    case '/':
                        if (top == 0)
                        {
                            return 0;
                        }

                        res = stack.Peek() / top;

                        if (res * top != stack.Peek())
                        {
                            return 0;
                        }

                        stack.Pop();
                        stack.Push(res);
                        break;
                }
            }

            res = stack.Pop();

            return res;
        }

        private static string generateExp(string selectors, List<string> nums, string operators,
            int numPos, int operatorPos)
        {
            Stack<string> stack = new Stack<string>();

            for (int i = 0; i < selectors.Length; i++)
            {
                if (selectors[i] == '0')
                {
                    stack.Push(nums[numPos++]);
                    continue;
                }

                string top = stack.Peek();
                stack.Pop();

                switch (operators[operatorPos++])
                {
                    case '+':
                        stack.Push("(" + stack.Pop() + "+" + top + ")");
                        break;
                    case '-':
                        stack.Push("(" + stack.Pop() + "-" + top + ")");
                        break;
                    case '*':
                        stack.Push("(" + stack.Pop() + "*" + top + ")");
                        break;
                    case '/':
                        stack.Push("(" + stack.Pop() + "/" + top + ")");
                        break;
                }
            }

            string exp = stack.Pop();

            return exp;
        }

        static void Main(string[] args)
        {
            List<string> nums = new List<string>() { "6", "6", "6", "6", "15", "50" };
            int target = 405;

            List<string> numsList = new List<string>() { "0", "1", "2", "3", "4", "5" };
            List<string> operatorsList = new List<string>() { "+", "-", "*", "/" };
            List<string> selectorsList = new List<string>() { "0", "1" };
            Stack<int> stack = new Stack<int>();
            List<string> expressions = new List<string>();

            var watch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 1; i <= 5; i++)
            {
                List<string> selectorsCombinations = generateCombinations("", selectorsList, 0, i + 1 + i, i + 1);
                List<string> numbersCombinations = generateCombinations("", numsList, 0, i + 1);
                List<string> operatorsCombinations = generateCombinations("", operatorsList, 0, i);

                for (int j = 0; j < selectorsCombinations.Count; j++)
                {
                    string selectorsCombination = selectorsCombinations[j];

                    for (int k = 0; k < numbersCombinations.Count; k++)
                    {
                        string numsbersCombination = numbersCombinations[k];
                        List<string> numbersCombinations2 = new List<string>();

                        for (int l = 0; l < numsbersCombination.Length; l++)
                        {
                            int index = Convert.ToInt32(numsbersCombination[l].ToString());
                            numbersCombinations2.Add(nums[index]);
                        }

                        for (int l = 0; l < operatorsCombinations.Count; l++)
                        {
                            string operatorsCombination = operatorsCombinations[l];

                            int res = evaluate(selectorsCombination, numbersCombinations2, operatorsCombination, 0, 0);

                            if (res == target || (res >= target - 10 && res <= target + 10))
                            {
                                string exp = generateExp(selectorsCombination, numbersCombinations2, operatorsCombination, 0, 0);
                                expressions.Add(exp + "=" + res);
                            }
                        }
                    }
                }
            }

            string closestExpression = "";
            int absDiff = int.MaxValue;

            for (int i = 0; i < expressions.Count; i++)
            {
                int res = Convert.ToInt32(expressions[i].Substring(expressions[i].IndexOf('=') + 1));

                if (res == target)
                {
                    closestExpression = expressions[i];
                    break;
                }
                else if (res > 0 && Math.Abs(res - target) < absDiff)
                {
                    absDiff = Math.Abs(res - target);
                    closestExpression = expressions[i];
                }
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds / 1000;

            Console.WriteLine("Expression: " + closestExpression);
            Console.WriteLine("Time: " + elapsedMs);
            Console.ReadKey();
        }
    }
}
 

跳伞者

工作人员
已加入
2019年4月6日
留言内容
2,604
地点
弗吉尼亚州切萨皮克
编程经验
10+
基于此分析运行,看起来不是堆栈在减慢您的速度,而是您将字符串解析为数字。
Screenshot_3.png


Screenshot_4.png
 

帕夫

知名会员
已加入
2016年10月4日
留言内容
47
编程经验
1-3
基于此分析运行,看起来不是堆栈在减慢您的速度,而是您将字符串解析为数字。
查看附件836

查看附件837
我创建了从int值而不是字符串值生成所有排列的方法,并且删除了要转换为int方法以进行计算的部分后,速度要快得多。

非常感谢你。我怎样才能使程序更快?

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp19
{
    class Program
    {
        private static List<List<int>> generate(int[] input)
        {
            List<List<int>> combinations = new List<List<int>>();
            int a = 0, b = 0, c = 0, d = 0, e = 0;

            for (int i = 0; i < input.Length; i++)
            {
                if (i == 0)
                {
                    a = 1;
                }
                else
                {
                    a = 0;
                }

                for (int j = a; j < input.Length; j++)
                {
                    if (j == 0)
                    {
                        b = 1;
                    }
                    else
                    {
                        b = 0;
                    }

                    string combination = i + "" + j;

                    if (combination.Distinct().Count() == combination.Length)
                    {
                        combinations.Add(new List<int>() { input[i], input[j] });
                    }

                    for (int k = b; k < input.Length; k++)
                    {
                        if (k == 0)
                        {
                            c = 1;
                        }
                        else
                        {
                            c = 0;
                        }

                        combination = i + "" + j + "" + k;

                        if (combination.Distinct().Count() == combination.Length)
                        {
                            combinations.Add(new List<int>() { input[i], input[j], input[k] });
                        }

                        for (int l = c; l < input.Length; l++)
                        {
                            if (l == 0)
                            {
                                d = 1;
                            }
                            else
                            {
                                d = 0;
                            }

                            combination = i + "" + j + "" + k + "" + l;

                            if (combination.Distinct().Count() == combination.Length)
                            {
                                combinations.Add(new List<int>() { input[i], input[j], input[k], input[l] });
                            }

                            for (int m = d; m < input.Length; m++)
                            {
                                if (m == 0)
                                {
                                    e = 1;
                                }
                                else
                                {
                                    e = 0;
                                }

                                combination = i + "" + j + "" + k + "" + l + "" + m;

                                if (combination.Distinct().Count() == combination.Length)
                                {
                                    combinations.Add(new List<int>() { input[i], input[j], input[k], input[l], input[m] });
                                }

                                for (int n = e; n < input.Length; n++)
                                {
                                    combination = i + "" + j + "" + k + "" + l + "" + m + "" + n;

                                    if (combination.Distinct().Count() == combination.Length)
                                    {
                                        combinations.Add(new List<int>() { input[i], input[j], input[k], input[l], input[m], input[n] });
                                    }
                                }
                            }
                        }
                    }
                }
            }

            combinations = combinations.OrderBy(x => x.Count).ToList();

            return combinations;
        }

        private static List<string> generateCombinations(string combination, List<string> input, int pos, int length,
            int inputCount = 0, List<string> combinations = null)
        {
            if (combinations == null)
            {
                combinations = new List<string>();
            }

            if (pos < length)
            {
                foreach (string num in input)
                {
                    generateCombinations(combination + num, input, pos + 1, length, inputCount, combinations);
                }
            }
            else
            {
                if (!combination.Contains("+") && !combination.Contains("-") && !combination.Contains("*") && !combination.Contains("/"))
                {
                    if (!input.Contains("2"))
                    {
                        if (combination.Count(s => s == '0') == inputCount && isSelectorValid(combination))
                        {
                            combinations.Add(combination);
                        }
                    }
                    else if (checkForDuplicate(combination))
                    {
                        combinations.Add(combination);
                    }
                }
                else
                {
                    combinations.Add(combination);
                }
            }

            return combinations;
        }

        private static bool checkForDuplicate(string combination)
        {
            return combination.Distinct().Count() == combination.Length;
        }

        private static bool isSelectorValid(string selectorsCombination)
        {
            int numValues = 0;

            for (int i = 0; i < selectorsCombination.Length; i++)
            {
                if (selectorsCombination[i].ToString().Equals("1"))
                {
                    if (--numValues <= 0)
                    {
                        return false;
                    }
                }
                else
                {
                    ++numValues;
                }
            }

            return numValues == 1;
        }

        private static int evaluate(string selectors, List<int> nums, string operators,
            int numPos, int operatorPos)
        {
            Stack<int> stack = new Stack<int>();
            int res = 0;

            for (int i = 0; i < selectors.Length; i++)
            {
                if (selectors[i] == '0')
                {
                    stack.Push(nums[numPos++]);
                    continue;
                }

                int top = stack.Peek();
                stack.Pop();

                switch (operators[operatorPos++])
                {
                    case '+':
                        stack.Push(stack.Pop() + top);
                        break;
                    case '-':
                        stack.Push(stack.Pop() - top);
                        break;
                    case '*':
                        stack.Push(stack.Pop() * top);
                        break;
                    case '/':
                        if (top == 0)
                        {
                            return 0;
                        }

                        //res = stack.Pop() / top;
                        res = stack.Peek() / top;

                        if (res * top != stack.Peek())
                        {
                            return 0;
                        }

                        stack.Pop();
                        stack.Push(res);
                        break;
                }
            }

            res = stack.Pop();

            return res;
        }

        private static string generateExp(string selectors, List<int> nums, string operators,
            int numPos, int operatorPos)
        {
            Stack<string> stack = new Stack<string>();

            for (int i = 0; i < selectors.Length; i++)
            {
                if (selectors[i] == '0')
                {
                    stack.Push(nums[numPos++].ToString());
                    continue;
                }

                string top = stack.Peek();
                stack.Pop();

                switch (operators[operatorPos++])
                {
                    case '+':
                        stack.Push("(" + stack.Pop() + "+" + top + ")");
                        break;
                    case '-':
                        stack.Push("(" + stack.Pop() + "-" + top + ")");
                        break;
                    case '*':
                        stack.Push("(" + stack.Pop() + "*" + top + ")");
                        break;
                    case '/':
                        stack.Push("(" + stack.Pop() + "/" + top + ")");
                        break;
                }
            }

            string exp = stack.Pop();

            return exp;
        }

        static void Main(string[] args)
        {
            int[] input = new int[]{ 0, 1, 2, 3, 4, 5 };

            List<List<int>> numbersCombinations = generate(input);

            List<int> nums = new List<int>() { 6, 6, 6, 6, 15, 50 };
            int target = 405;

            List<string> operatorsList = new List<string>() { "+", "-", "*", "/" };
            List<string> selectorsList = new List<string>() { "0", "1" };
            List<string> expressions = new List<string>();

            var watch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 5; i <= 5; i++)
            {
                List<string> selectorsCombinations = generateCombinations("", selectorsList, 0, i + 1 + i, i + 1);
                List<string> operatorsCombinations = generateCombinations("", operatorsList, 0, i);
                List<List<int>> numbersCombinations2 = numbersCombinations.Where(s => s.Count == i + 1).ToList();

                for (int j = 0; j < selectorsCombinations.Count; j++)
                {
                    string selectorsCombination = selectorsCombinations[j];

                    for (int k = 0; k < numbersCombinations2.Count; k++)
                    {
                        List<int> numbersCombination = numbersCombinations2[k];
                        List<int> numbersCombinations3 = new List<int>();

                        for (int l = 0; l < numbersCombination.Count; l++)
                        {
                            numbersCombinations3.Add(nums[numbersCombination[l]]);
                        }

                        for (int l = 0; l < operatorsCombinations.Count; l++)
                        {
                            string operatorsCombination = operatorsCombinations[l];

                            int res = evaluate(selectorsCombination, numbersCombinations3, operatorsCombination, 0, 0);

                            if (res == target || (res >= target - 10 && res <= target + 10))
                            {
                                string exp = generateExp(selectorsCombination, numbersCombinations3, operatorsCombination, 0, 0);
                                expressions.Add(exp + "=" + res);
                            }
                        }
                    }
                }
            }

            string closestExpression = "";
            int absDiff = int.MaxValue;

            for (int i = 0; i < expressions.Count; i++)
            {
                int res = Convert.ToInt32(expressions[i].Substring(expressions[i].IndexOf('=') + 1));

                if (res == target)
                {
                    closestExpression = expressions[i];
                    break;
                }
                else if (res > 0 && Math.Abs(res - target) < absDiff)
                {
                    absDiff = Math.Abs(res - target);
                    closestExpression = expressions[i];
                }
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds / 1000;

            Console.WriteLine("Expression: " + closestExpression);
            Console.WriteLine("Time: " + elapsedMs);
            Console.ReadKey();
        }
    }
}
 

跳伞者

工作人员
已加入
2019年4月6日
留言内容
2,604
地点
弗吉尼亚州切萨皮克
编程经验
10+
只是盯着东西,似乎您继续这样做:
C#:
组合.Distinct().Count() == combination.Length
on a string to determine if there all the characters in the string is unique. Furthermore, it looks like you are using a string as a data structure. Why not just use HashSet<char> if the string truly contains a "combination"。回想一下,在组合中,项目的顺序无关紧要。如果顺序确实很重要,那么它的一个"permutation", not a "combination".
 

帕夫

知名会员
已加入
2016年10月4日
留言内容
47
编程经验
1-3
只是盯着东西,似乎您继续这样做:
C#:
组合.Distinct().Count() == combination.Length
on a string to determine if there all the characters in the string is unique. Furthermore, it looks like you are using a string as a data structure. Why not just use HashSet<char> if the string truly contains a "combination"。回想一下,在组合中,项目的顺序无关紧要。如果顺序确实很重要,那么它的一个"permutation", not a "combination".

我删除了检查字符串的那一部分,现在我正在检查索引。为了生成运算符排列,我仍然使用generateCombinations方法,因为排列必须是字符串。
我还删除了combination.Count(s =>s =='0'),我使用for循环获取零计数。
为了使程序更快,我还应该更改什么?

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp19
{
    class Program
    {
        private static List<List<int>> generate(int[] input)
        {
            List<List<int>> combinations = new List<List<int>>();
            int a = 0, b = 0, c = 0, d = 0, e = 0;

            for (int i = 0; i < input.Length; i++)
            {
                if (i == 0)
                {
                    a = 1;
                }
                else
                {
                    a = 0;
                }

                for (int j = a; j < input.Length; j++)
                {
                    if (j == 0)
                    {
                        b = 1;
                    }
                    else
                    {
                        b = 0;
                    }

                    if (i != j)
                    {
                        combinations.Add(new List<int>() { input[i], input[j] });
                    }

                    for (int k = b; k < input.Length; k++)
                    {
                        if (k == 0)
                        {
                            c = 1;
                        }
                        else
                        {
                            c = 0;
                        }

                        if (i != j && i != k && j != k)
                        {
                            combinations.Add(new List<int>() { input[i], input[j], input[k] });
                        }

                        for (int l = c; l < input.Length; l++)
                        {
                            if (l == 0)
                            {
                                d = 1;
                            }
                            else
                            {
                                d = 0;
                            }

                            if (i != j && i != k && i != l && j != k && j != l && k != l)
                            {
                                combinations.Add(new List<int>() { input[i], input[j], input[k], input[l] });
                            }

                            for (int m = d; m < input.Length; m++)
                            {
                                if (m == 0)
                                {
                                    e = 1;
                                }
                                else
                                {
                                    e = 0;
                                }

                                if (i != j && i != k && i != l && i != m && j != k && j != l && j != m && k != l && k != m && l != m)
                                {
                                    combinations.Add(new List<int>() { input[i], input[j], input[k], input[l], input[m] });
                                }

                                for (int n = e; n < input.Length; n++)
                                {
                                    if (i != j && i != k && i != l && i != m && i != n && j != k && j != l && j != m
                                        && j != n && k != l && k != m && k != n && l != m && l != n && m != n)
                                    {
                                        combinations.Add(new List<int>() { input[i], input[j], input[k], input[l], input[m], input[n] });
                                    }
                                }
                            }
                        }
                    }
                }
            }

            combinations = combinations.OrderBy(x => x.Count).ToList();

            return combinations;
        }

        private static List<string> generateCombinations(string combination, List<string> input, int pos, int length,
            int inputCount = 0, List<string> combinations = null)
        {
            if (combinations == null)
            {
                combinations = new List<string>();
            }

            if (pos < length)
            {
                foreach (string num in input)
                {
                    generateCombinations(combination + num, input, pos + 1, length, inputCount, combinations);
                }
            }
            else
            {
                if (input[0].Equals("0") || input[0].Equals("1"))
                {
                    int count = 0;

                    for (int i = 0; i < combination.Length; i++)
                    {
                        if (combination[i] == '0')
                        {
                            count++;
                        }
                    }

                    if (count == inputCount && isSelectorValid(combination))
                    {
                        combinations.Add(combination);
                    }
                }
                else
                {
                    combinations.Add(combination);
                }
            }

            return combinations;
        }

        private static bool isSelectorValid(string selectorsCombination)
        {
            int numValues = 0;

            for (int i = 0; i < selectorsCombination.Length; i++)
            {
                if (selectorsCombination[i].ToString().Equals("1"))
                {
                    if (--numValues <= 0)
                    {
                        return false;
                    }
                }
                else
                {
                    ++numValues;
                }
            }

            return numValues == 1;
        }

        private static int evaluate(string selectors, List<int> nums, string operators,
            int numPos, int operatorPos)
        {
            Stack<int> stack = new Stack<int>();
            int res = 0;

            for (int i = 0; i < selectors.Length; i++)
            {
                if (selectors[i] == '0')
                {
                    stack.Push(nums[numPos++]);
                    continue;
                }

                int top = stack.Peek();
                stack.Pop();

                switch (operators[operatorPos++])
                {
                    case '+':
                        stack.Push(stack.Pop() + top);
                        break;
                    case '-':
                        stack.Push(stack.Pop() - top);
                        break;
                    case '*':
                        stack.Push(stack.Pop() * top);
                        break;
                    case '/':
                        if (top == 0)
                        {
                            return 0;
                        }

                        res = stack.Peek() / top;

                        if (res * top != stack.Peek())
                        {
                            return 0;
                        }

                        stack.Pop();
                        stack.Push(res);
                        break;
                }
            }

            res = stack.Pop();

            return res;
        }

        private static string generateExp(string selectors, List<int> nums, string operators,
            int numPos, int operatorPos)
        {
            Stack<string> stack = new Stack<string>();

            for (int i = 0; i < selectors.Length; i++)
            {
                if (selectors[i] == '0')
                {
                    stack.Push(nums[numPos++].ToString());
                    continue;
                }

                string top = stack.Peek();
                stack.Pop();

                switch (operators[operatorPos++])
                {
                    case '+':
                        stack.Push("(" + stack.Pop() + "+" + top + ")");
                        break;
                    case '-':
                        stack.Push("(" + stack.Pop() + "-" + top + ")");
                        break;
                    case '*':
                        stack.Push("(" + stack.Pop() + "*" + top + ")");
                        break;
                    case '/':
                        stack.Push("(" + stack.Pop() + "/" + top + ")");
                        break;
                }
            }

            string exp = stack.Pop();

            return exp;
        }

        static void Main(string[] args)
        {
            int[] input = new int[]{ 0, 1, 2, 3, 4, 5 };

            List<List<int>> numbersCombinations = generate(input);

            List<int> nums = new List<int>() { 6, 6, 6, 6, 15, 50 };
            int target = 405;

            List<string> numsList = new List<string>() { "0", "1", "2", "3", "4", "5" };
            List<string> operatorsList = new List<string>() { "+", "-", "*", "/" };
            List<string> selectorsList = new List<string>() { "0", "1" };
            Stack<int> stack = new Stack<int>();
            List<string> expressions = new List<string>();

            var watch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 1; i <= 5; i++)
            {
                List<string> selectorsCombinations = generateCombinations("", selectorsList, 0, i + 1 + i, i + 1);
                List<string> operatorsCombinations = generateCombinations("", operatorsList, 0, i);
                List<List<int>> numbersCombinations2 = numbersCombinations.Where(s => s.Count == i + 1).ToList();

                for (int j = 0; j < selectorsCombinations.Count; j++)
                {
                    string selectorsCombination = selectorsCombinations[j];

                    for (int k = 0; k < numbersCombinations2.Count; k++)
                    {
                        List<int> numbersCombination = numbersCombinations2[k];
                        List<int> numbersCombinations3 = new List<int>();

                        for (int l = 0; l < numbersCombination.Count; l++)
                        {
                            numbersCombinations3.Add(nums[numbersCombination[l]]);
                        }

                        for (int l = 0; l < operatorsCombinations.Count; l++)
                        {
                            string operatorsCombination = operatorsCombinations[l];

                            int res = evaluate(selectorsCombination, numbersCombinations3, operatorsCombination, 0, 0);

                            if (res == target || (res >= target - 10 && res <= target + 10))
                            {
                                string exp = generateExp(selectorsCombination, numbersCombinations3, operatorsCombination, 0, 0);
                                expressions.Add(exp + "=" + res);
                            }
                        }
                    }
                }
            }
            
            string closestExpression = "";
            int absDiff = int.MaxValue;

            for (int i = 0; i < expressions.Count; i++)
            {
                int res = Convert.ToInt32(expressions[i].Substring(expressions[i].IndexOf('=') + 1));

                if (res == target)
                {
                    closestExpression = expressions[i];
                    break;
                }
                else if (res > 0 && Math.Abs(res - target) < absDiff)
                {
                    absDiff = Math.Abs(res - target);
                    closestExpression = expressions[i];
                }
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds / 1000;

            Console.WriteLine("Expression: " + closestExpression);
            Console.WriteLine("Time: " + elapsedMs);
            Console.ReadKey();
        }
    }
}
 

跳伞者

工作人员
已加入
2019年4月6日
留言内容
2,604
地点
弗吉尼亚州切萨皮克
编程经验
10+
最好的判断方法是对您的代码运行探查器,并找到热点。还要注意有很多垃圾收集事件的地方。

就个人而言,我认为可以加快排列和组合的生成,并且应该选择更好的数据结构。这只是基于代码看起来有多复杂以及所有字符串连接的直觉。探查器将再次为您提供更多见解。在MSDN中查看。有.NET Framework有关提高性能的建议。

我记得,如果您要执行许多字符串连接操作,那么其中一项建议就是使用StringBuilder。这是因为它优化了创建最终字符串所需的分配数量,而不是在串联中为您创建的每对字符串分配一个新字符串。
 

跳伞者

工作人员
已加入
2019年4月6日
留言内容
2,604
地点
弗吉尼亚州切萨皮克
编程经验
10+
排列必须是字符串
真的吗?那么,这些世纪以来,数学家一直在做错所有的事情吗?什么是字符串,但字符列表?为什么它不能是操作列表?
 

跳伞者

工作人员
已加入
2019年4月6日
留言内容
2,604
地点
弗吉尼亚州切萨皮克
编程经验
10+
我在Wikipedia上读到的Countdown游戏是不允许使用负数和分数。我在您的代码中看到了您尝试防止小数的地方,但是在防止出现负面结果的地方,我一定很想念。

根据我的阅读,对于游戏节目,参赛者的目标是至少提出一种解决方案。对于此编程任务,是相同的还是应该列出所有可能的解决方案?对于互为镜像的解决方案(例如5 + 6 == 6 + 5),应同时列出或仅列出两者?
 

帕夫

知名会员
已加入
2016年10月4日
留言内容
47
编程经验
1-3
我在Wikipedia上读到的Countdown游戏是不允许使用负数和分数。我在您的代码中看到了您尝试防止小数的地方,但是在防止出现负面结果的地方,我一定很想念。

根据我的阅读,对于游戏节目,参赛者的目标是至少提出一种解决方案。对于此编程任务,是相同的还是应该列出所有可能的解决方案?对于互为镜像的解决方案(例如5 + 6 == 6 + 5),应同时列出或仅列出两者?

当解决方案是这样5 + 6 == 6 + 5时,只应列出一个
 
最佳 底部