.net C# 实现任意List的笛卡尔乘积算法代码

所属分类: 软件编程 / C#教程 阅读数: 80
收藏 0 赞 0 分享
可以扩展到多个集合的情况。类似的例子有,如果A表示某学校学生的集合,B表示该学校所有课程的集合,则A与B的笛卡尔积表示所有可能的选课情况

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace 算法
{
    public static class 算法
    {
        /// <summary>
        /// 笛卡尔乘积
        /// </summary>
        public static List<List<T>> CartesianProduct<T>(this List<List<T>> lstSplit)
        {
            int count = 1;
            lstSplit.ForEach(item => count *= item.Count);
            //count = lstSplit.Aggregate(1, (result, next) => result * next.Count);

            var lstResult = new List<List<T>>();

            for (int i = 0; i < count; ++i)
            {
                var lstTemp = new List<T>();
                int j = 1;
                lstSplit.ForEach(item =>
                {
                    j *= item.Count;
                    lstTemp.Add(item[(i / (count / j)) % item.Count]);
                });
                lstResult.Add(lstTemp);
            }
            return lstResult;
        }
    }

    class Program
    {
        public static void Main()
        {
            StringDemo();
            根据Sector生成Routing的Demo();
            根据Sector生成Routing的Demo2();
        }

        /// <summary>
        /// 简单字符串 笛卡尔乘积
        /// </summary>
        private static void StringDemo()
        {
            var lstSource = new List<List<string>>
            {
                new List<string>() { "A","B","C"},
                new List<string>() { "D","E","F"},
                new List<string>() { "G","H","I"},
            };

            var sw = new Stopwatch();
            sw.Start();
            var lstResult = lstSource.CartesianProduct();
            Console.WriteLine(sw.Elapsed);
        }


        private static void 根据Sector生成Routing的Demo()
        {
            //默认允许输入多个BookingClass,表示使用任意一个都可以。
            var lstSectorDef = new List<Sector>
            {
                new Sector{ SeqNO=1, BookingClass="A/A1/A2"},
                new Sector{ SeqNO=2, BookingClass="B/B1/B2"},
                new Sector{ SeqNO=3, BookingClass="C/C1/C2"},
                //.....数量不定
            };


            var sw = new Stopwatch();
            sw.Start();

            var lstSectorGroup = new List<List<Sector>>();
            lstSectorDef.ForEach(item =>
            {
                var lstSector = new List<Sector>();
                foreach (var bookingClass in item.BookingClass.Split('/'))
                {
                    var sector = item.Clone();
                    sector.BookingClass = bookingClass;

                    lstSector.Add(sector);
                }
                lstSectorGroup.Add(lstSector);
            });

            var lstRouting = lstSectorGroup.CartesianProduct();

            Console.WriteLine(sw.Elapsed);
        }


        private static void 根据Sector生成Routing的Demo2()
        {
            //默认允许输入多个BookingClass,表示使用任意一个都可以。
            var lstSectorDef = new List<Sector>
            {
                new Sector{ SeqNO=1, BookingClass="A1/A2/A3"},
                new Sector{ SeqNO=2, BookingClass="B1/B2/B3"},
                new Sector{ SeqNO=3, BookingClass="C1/C2/C3"},
                //.....数量不定
            };

            var sw = new Stopwatch();
            sw.Start();

            var lstTemp = new List<List<string>>();
            lstSectorDef.ForEach(item =>
            {
                lstTemp.Add(item.BookingClass.Split('/').ToList());
            });

            var lstBookingClassGroup = lstTemp.CartesianProduct();

            var lstRouting = new List<List<Sector>>();
            for (int i = 0; i < lstBookingClassGroup.Count; i++)
            {
                var lstSector = new List<Sector>();
                for (int j = 0; j < lstSectorDef.Count; j++)
                {
                    var sector = lstSectorDef[j].Clone();
                    sector.BookingClass = lstBookingClassGroup[i][j];
                    lstSector.Add(sector);
                }
                lstRouting.Add(lstSector);
            }

            Console.WriteLine(sw.Elapsed);
        }

 

    }

    [DebuggerDisplay("Sector:SeqNO={SeqNO},BookingClass={BookingClass}")]
    public class Sector
    {
        public int SeqNO { get; set; }
        public string BookingClass { get; set; }

        public Sector Clone()
        {
            return this.MemberwiseClone() as Sector;
        }
    }
}

更多精彩内容其他人还在看

C# SendInput 模拟鼠标操作的实现方法

C# SendInput 模拟鼠标操作的实现方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中 paint()与Onpaint()的区别

paint是事件onpaint方法onpaint方法是调用paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的:
收藏 0 赞 0 分享

c#中GetType()与Typeof()的区别

c#中GetType()与Typeof()的区别,需要的朋友可以参考一下
收藏 0 赞 0 分享

将字符串转换成System.Drawing.Color类型的方法

将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# 抓取网页内容的方法

C# 抓取网页内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于C#后台调用跨域MVC服务及带Cookie验证的实现

本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
收藏 0 赞 0 分享

使用C#获取远程图片 Form用户名与密码Authorization认证的实现

本篇文章介绍了,使用C#获取远程图片 Form用户名与密码Authorization认证的实现。需要的朋友参考下
收藏 0 赞 0 分享

Winform跨线程操作的简单方法

线程间操作无效:从不是创建控件“label1”的线程访问它
收藏 0 赞 0 分享

C# WINFORM 强制让窗体获得焦点的方法代码

C# WINFORM 强制让窗体获得焦点的方法代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中方括号[]的语法及作用介绍

C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。
收藏 0 赞 0 分享
查看更多