C#中實現(xiàn)任意List的全組合算法代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 算法
{
class 全組合算法
{
[Flags]
public enum PersonType
{
Audit = 1,
Child = 2,
Senior = 4
}
public static void Run(string[] args)
{
var lstSource = GetEnumList<PersonType>();
var lstComb = FullCombination(lstSource);
var lstResult = new List<PersonType>();
lstComb.ForEach(item =>
{
lstResult.Add(item.Aggregate((result, source) => result | source));
});
}
public static List<T> GetEnumList<T>()
{
var lst = new List<T>();
foreach (T item in Enum.GetValues(typeof(T)))
{
lst.Add(item);
}
return lst;
}
//全組合算法
public static List<List<T>> FullCombination<T>(List<T> lstSource)
{
var n = lstSource.Count;
var max = 1 << n;
var lstResult = new List<List<T>>();
for (var i = 0; i < max; i++)
{
var lstTemp = new List<T>();
for (var j = 0; j < n; j++)
{
if ((i >> j & 1) > 0)
{
lstTemp.Add(lstSource[j]);
}
}
lstResult.Add(lstTemp);
}
lstResult.RemoveAt(0);
return lstResult;
}
}
}
- C#生成指定范圍內(nèi)的不重復(fù)隨機(jī)數(shù)
- C#生成不重復(fù)隨機(jī)字符串類
- C#生成唯一不重復(fù)訂單號
- C#隨機(jī)生成不重復(fù)字符串的兩個不錯方法
- C#不重復(fù)輸出一個數(shù)組中所有元素的方法
- C#實現(xiàn)在購物車系統(tǒng)中生成不重復(fù)訂單號的方法
- c# 兩個數(shù)組比較,將重復(fù)部分去掉,返回不重復(fù)部分的實現(xiàn)
- C#實現(xiàn)排列組合算法完整實例
- C#查找字符串所有排列組合的方法
- C#實現(xiàn)組合排列的方法
- 詳解C#的排列組合
- C#實現(xiàn)生成所有不重復(fù)的組合功能示例
相關(guān)文章
C#?Windows?Forms中實現(xiàn)控件之間的連接線的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何在C#?Windows?Forms應(yīng)用程序中實現(xiàn)繪圖工具中多個控件之間的連接線功能,文中的示例代碼講解詳細(xì),需要的可以參考下2024-02-02
C# MVC模式中應(yīng)該怎樣區(qū)分應(yīng)用程序邏輯(Controller層)和業(yè)務(wù)邏輯(Model層)?
這篇文章主要介紹了C# MVC模式中應(yīng)該怎樣區(qū)分應(yīng)用程序邏輯(Controller層)和業(yè)務(wù)邏輯(Model層)?,這也小編做.NET項目時經(jīng)常思考和讓人混亂的一個問題,這篇文章寫的挺好,一下清晰了許多,需要的朋友可以參考下2015-06-06

