c# - 将数字字符串拆分为单位数和两位数的可能组合

标签 c# algorithm

我有一个数字字符串,我想将其拆分为单位数和两位数数字的可能组合。

例如:拆分字符串“59145”后,我想要以下组合:

5 9 1 4 5
5 9 1 45
5 9 14 5
5 91 4 5
5 91 45
59 1 4 5
59 1 45
59 14 5

我当前的代码如下所示:

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var combination in SplitNumbers("59145"))
            {
                foreach (var number in combination)
                {
                    Console.Write(number + " ");
                }
                Console.WriteLine();
            }

            Console.Read();
        }

        static IEnumerable<List<string>> SplitNumbers(string input)
        {
            int numberCombinations = Fibonacci(input.Length + 1);

            List<string> combination;
            string temp;
            int count;

            for (int i = 0; i < numberCombinations; i++)
            {
                combination = new List<string>();
                temp = input;
                count = 0;

                while (temp.Length > 0)
                {
                    combination.Add(temp.Substring(0, combinations[input.Length][i][count]));
                    temp = temp.Remove(0, combinations[input.Length][i][count]);
                    count++;
                }
                yield return combination;
            }
        }

        public static int Fibonacci(int n)
        {
            int a = 0;
            int b = 1;

            for (int i = 0; i < n; i++)
            {
                int temp = a;
                a = b;
                b = temp + b;
            }
            return a;
        }

        static int[][][] combinations = new int[][][]
        {
            //0 numbers
            new int[][]
            {
                new int[]{0}
            },
            //1 number
            new int[][]
            {
                new int[]{1}
            },
            //2 numbers
            new int[][]
            {
                new int[]{1,1},
                new int[]{2}
            },
            //3 numbers
            new int[][]
            {
                new int[]{1,1,1},
                new int[]{1,2},
                new int[]{2,1}
            },
            //4 numbers
            new int[][]
            {
                new int[]{1,1,1,1},
                new int[]{1,1,2},
                new int[]{1,2,1},
                new int[]{2,1,1},
                new int[]{2,2}
            },
            //5 numbers
            new int[][]
            {
                new int[]{1,1,1,1,1},
                new int[]{1,1,1,2},
                new int[]{1,1,2,1},
                new int[]{1,2,1,1},
                new int[]{1,2,2},
                new int[]{2,1,1,1},
                new int[]{2,1,2},
                new int[]{2,2,1}
            }
        };
    }
}

我的问题是我必须对每种可能的组合进行硬编码。我确信有可能以编程方式解决这个问题,但目前我不知道如何做到这一点。

最佳答案

使用递归,可以这样完成:

static IEnumerable<List<string>> Split(string input)
{
    return Split(input, new List<string>());
}

static IEnumerable<List<string>> Split(string input, List<string> current)
{
    if (input.Length == 0)
    {
        yield return current;
    }

    if (input.Length >= 1)
    {
        var copy = current.ToList();
        copy.Add(input.Substring(0, 1));
        foreach (var r in Split(input.Substring(1), copy))
            yield return r;
    }

    if (input.Length >= 2)
    {
        var copy = current.ToList();
        copy.Add(input.Substring(0, 2));
        foreach (var r in Split(input.Substring(2), copy))
            yield return r;
    }
}

这将打印项目列表:

foreach (var r in Split("59145"))
    Console.WriteLine(string.Join(",", r));

这是一个working fiddle .

关于c# - 将数字字符串拆分为单位数和两位数的可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25380719/

相关文章:

c# - 读/写压缩二进制数据

c# - 如何在 asp.net core 2.1 中使用自定义消息设置状态码?

c# - 在 O(n) 时间内对序列进行排序

algorithm - 最大矩形集覆盖

c# - 为什么这个双击检测代码不可靠?

c# - 如何使用C#迭代 Elasticsearch 桶

c# - 如何查看程序集的强名称?

algorithm - 在每个查询的图中为 u 和 v 节点之间的路径找到一些东西(最小/最大/唯一)

python - 优化python中的递归

python - 在循环迭代中另外比较两个数字