.net C# 實(shí)現(xiàn)任意List的笛卡爾乘積算法代碼
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();
根據(jù)Sector生成Routing的Demo();
根據(jù)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 根據(jù)Sector生成Routing的Demo()
{
//默認(rèn)允許輸入多個(gè)BookingClass,表示使用任意一個(gè)都可以。
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"},
//.....數(shù)量不定
};
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 根據(jù)Sector生成Routing的Demo2()
{
//默認(rèn)允許輸入多個(gè)BookingClass,表示使用任意一個(gè)都可以。
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"},
//.....數(shù)量不定
};
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# 生成高質(zhì)量縮略圖程序—終極算法
- C#的3DES加密解密算法實(shí)例代碼
- C#常見算法面試題小結(jié)
- asp.net(c#)兩種隨機(jī)數(shù)的算法,可用抽考題
- c#漢諾塔的遞歸算法與解析
- C#加密算法匯總(推薦)
- 基于C#代碼實(shí)現(xiàn)九宮格算法橫豎都等于4
- c# 實(shí)現(xiàn)MD5,SHA1,SHA256,SHA512等常用加密算法源代碼
- 基于私鑰加密公鑰解密的RSA算法C#實(shí)現(xiàn)方法
- c#哈希算法的實(shí)現(xiàn)方法及思路
- C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘五 棧和隊(duì)列
- C#實(shí)現(xiàn)的海盜分金算法實(shí)例
相關(guān)文章
C#使用百度Ueditor富文本框?qū)崿F(xiàn)上傳文件
這篇文章主要為大家詳細(xì)介紹了C#如何使用百度Ueditor富文本框?qū)崿F(xiàn)上傳文件(圖片,視頻等),文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-07-07

C# websocket及時(shí)通信協(xié)議的實(shí)現(xiàn)方法示例