面向云技术架构 - 痴者工良

  • 首页
  • 工良写的电子书
    • kubernetes 教程
    • 从 C# 入门 Kafka
    • 多线程和异步
    • 动态编程-反射、特性、AOP
    • 表达式树
  • 本站文章导航
  • 隐私政策
愿有人陪你颠沛流离
遇到能让你付出的事物或者人,都是一种运气。
能遇到,就该珍惜。或许你们最终没能在一起,但你会切实地感受到力量。
正因为这样,那段相遇才变得有价值,才没有辜负这世间的每一段相遇。
  1. 首页
  2. .NET
  3. 正文

C# 多维笛卡尔积、组合算法

2022年11月2日 414点热度 0人点赞 0条评论
内容纲要

有以下 N 个因子:

        new List<string>{"A","B","C"},      // 位置 0
        new List<string>{"1","2","3"},      // 位置 1
        new List<string>{"①","②","③"},
        new List<string>{"Ⅰ","Ⅱ","Ⅲ"},
        new List<string>{"一","二","三"},

可以组成多个 n 长度的数组,如:

A   1   ①   Ⅰ   一

M * N * Z * X * Y

file

每个因子类型的位置都是固定的,现在要计算每一种组合并输出,并且最终有序。

方法一

从方法是从别人 Go 语言中的代码中学到,然后笔者使用 C# 编写实现的。
Go 版本地址:https://www.codetd.com/article/11896053

void Main()
{

    List<List<string>> sourceElements = new List<List<string>>()
    {
        new List<string>{"A","B","C"},
        new List<string>{"1","2","3"},
        new List<string>{"①","②","③"},
        new List<string>{"Ⅰ","Ⅱ","Ⅲ"},
        new List<string>{"一","二","三"},
    };

    IEnumerable<string[]> targetElments = Product(sourceElements);
    foreach (var item in targetElments)
    {
        Console.WriteLine(JsonSerializer.Serialize(item));
    }
}

public List<string[]> Product(List<List<string>> sets)
{
    Func<int, int> lens = (i => sets[i].Count);
    var product = new List<string[]>();

    for (var ix = new int[sets.Count]; ix[0] < lens(0); NextIndex(ix, lens))
    {
        var r = new List<string>();
        for (int j = 0; j < ix.Length; j++)
        {
            var k = ix[j];
            r.Add(sets[j][k]);
        }
        product.Add(r.ToArray());
    }
    return product;
}

public void NextIndex(int[] ix, Func<int, int> lens)
{
    for (int j = ix.Length - 1; j >= 0; j--)
    {
        ix[j]++;
        if (j == 0 || ix[j] < lens(j))
        {
            return;
        }
        ix[j] = 0;
    }
}

方法二

缺点多,产生了大量的对象、数组,这是笔者第一个版本写的,然后使用了第一种方法做优化。

void Main()
{
    List<List<string>> sourceElements = new List<List<string>>()
    {
        new List<string>{"A","B","C"},
        new List<string>{"1","2","3"},
        new List<string>{"①","②","③"},
        new List<string>{"Ⅰ","Ⅱ","Ⅲ"},
        new List<string>{"一","二","三"},
    };

    List<string[]> targetElments = new List<string[]>();
    var length = sourceElements.Count;
    // 无效的数量
    var invalidCount = 0;
    for (var i = 0; i < length; i++)
    {
        // A、B、C 
        // 1、2、3
        var sourceElment = sourceElements[i];
        var sourceCount = sourceElment.Count;
        if (targetElments.Count == 0)
        {
            foreach (var item in sourceElment)
            {
                var array = new string[length];
                array[0] = item;
                targetElments.Add(array);
            }
        }
        else
        {
            // A , null, null, null, null 
            var targetCount = targetElments.Count;
            for (int j = 0; j < targetCount; j++)
            {
                var targetElment = targetElments[j];
                // 当前因素所在位置
                var position = i;
                for (int k = 0; k < sourceCount; k++)
                {
                    // A , null, null, null, null | 1 | "1"
                    Build(targetElments, targetElment, position, sourceElment[k]);
                }
            }
            targetElments.RemoveRange(0,targetCount);
        }
    }
    foreach (var item in targetElments)
    {
        Console.WriteLine(JsonSerializer.Serialize(item));
    }

}

public void Build(List<string[]> elments, string[] source, int position, string value)
{
    var newArray = new string[source.Length];
    Array.Copy(source, newArray, source.Length);
    newArray[position] = value;
    elments.Add(newArray);
}

然后在实际场景中根据算法对部分代码进行优化。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: c 多维 笛卡尔 算法 组合
最后更新:2022年11月2日

痴者工良

高级程序员劝退师

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

文章目录
  • 方法一
  • 方法二

COPYRIGHT © 2022 whuanle.cn. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

粤ICP备18051778号

粤公网安备 44030902003257号