• 笛卡尔积求二维数组所有组合


    static void Main(string[] args)
    {
        List<List<string>> allList = new List<List<string>>
        {
            new List<string>{ "a","b","c"},
            new List<string>{ "1","2","3"},
            new List<string>{ "A","B","C","D","E"}
        };
        List<string> result = new List<string>();
        Fun(allList, 0, result, string.Empty);
        foreach (var item in result)
        {
            Console.Write(item + "_");
        }
    }
    
    /// <param name="list">二维数组</param>
    /// <param name="count">二维数组的索引</param>
    /// <param name="result">返回的结果集</param>
    /// <param name="str">单个组合</param>
    public static void Fun(List<List<string>> list, int count, List<string> result, string str)
    {
        //获取当前数组
        List<string> current = list[count];
        foreach (var item in current)
        {
            if (count + 1 < list.Count)
            {
                //跳至下一个数组
                Fun(list, count + 1, result, str + item);
            }
            else
            {
                //到达最后一个数组,将拼接的单个组合添加到结果集
                result.Add(str + item);
            }
        }
    }
    

  • 相关阅读:
    poj 2942 Knights of the Round Table 双连通分量
    zoj 2588 Burning Bridges 桥
    desin pattern
    android
    ubuntu
    centos
    android布局
    gradle
    好站
    tomcat datasource
  • 原文地址:https://www.cnblogs.com/gygg/p/13576442.html
Copyright © 2020-2023  润新知