c# 排列组合代码类
生活随笔
收集整理的這篇文章主要介紹了
c# 排列组合代码类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/// <summary>
/// 排列組件算法類
/// </summary>
/// <typeparam name="T"></typeparam>
public class PermutationAndCombination<T>
{
/// <summary>
/// 交換兩個(gè)變量
/// </summary>
/// <param name="a">變量1</param>
/// <param name="b">變量2</param>
public static void Swap(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
/// <summary>
/// 遞歸算法求數(shù)組的組合(私有成員)
/// </summary>
/// <param name="list">返回的范型</param>
/// <param name="t">所求數(shù)組</param>
/// <param name="n">輔助變量</param>
/// <param name="m">輔助變量</param>
/// <param name="b">輔助數(shù)組</param>
/// <param name="M">輔助變量M</param>
private static void GetCombination(ref List<T[]> list, T[] t, int n, int m, int[] b, int M)
{
for (int i = n; i >= m; i--)
{
b[m - ] = i - ;
if (m > )
{
GetCombination(ref list, t, i - , m - , b, M);
}
else
{
if (list == null)
{
list = new List<T[]>();
}
T[] temp = new T[M];
for (int j = ; j < b.Length; j++)
{
temp[j] = t[b[j]];
}
list.Add(temp);
}
}
}
/// <summary>
/// 遞歸算法求排列(私有成員)
/// </summary>
/// <param name="list">返回的列表</param>
/// <param name="t">所求數(shù)組</param>
/// <param name="startIndex">起始標(biāo)號(hào)</param>
/// <param name="endIndex">結(jié)束標(biāo)號(hào)</param>
private static void GetPermutation(ref List<T[]> list, T[] t, int startIndex, int endIndex)
{
if (startIndex == endIndex)
{
if (list == null)
{
list = new List<T[]>();
}
T[] temp = new T[t.Length];
t.CopyTo(temp, );
list.Add(temp);
}
else
{
for (int i = startIndex; i <= endIndex; i++)
{
Swap(ref t[startIndex], ref t[i]);
GetPermutation(ref list, t, startIndex + , endIndex);
Swap(ref t[startIndex], ref t[i]);
}
}
}
/// <summary>
/// 求從起始標(biāo)號(hào)到結(jié)束標(biāo)號(hào)的排列,其余元素不變
/// </summary>
/// <param name="t">所求數(shù)組</param>
/// <param name="startIndex">起始標(biāo)號(hào)</param>
/// <param name="endIndex">結(jié)束標(biāo)號(hào)</param>
/// <returns>從起始標(biāo)號(hào)到結(jié)束標(biāo)號(hào)排列的范型</returns>
public static List<T[]> GetPermutation(T[] t, int startIndex, int endIndex)
{
if (startIndex < || endIndex > t.Length - )
{
return null;
}
List<T[]> list = new List<T[]>();
GetPermutation(ref list, t, startIndex, endIndex);
return list;
}
/// <summary>
/// 返回?cái)?shù)組所有元素的全排列
/// </summary>
/// <param name="t">所求數(shù)組</param>
/// <returns>全排列的范型</returns>
public static List<T[]> GetPermutation(T[] t)
{
return GetPermutation(t, , t.Length - );
}
/// <summary>
/// 求數(shù)組中n個(gè)元素的排列
/// </summary>
/// <param name="t">所求數(shù)組</param>
/// <param name="n">元素個(gè)數(shù)</param>
/// <returns>數(shù)組中n個(gè)元素的排列</returns>
public static List<T[]> GetPermutation(T[] t, int n)
{
if (n > t.Length)
{
return null;
}
List<T[]> list = new List<T[]>();
List<T[]> c = GetCombination(t, n);
for (int i = ; i < c.Count; i++)
{
List<T[]> l = new List<T[]>();
GetPermutation(ref l, c[i], , n - );
list.AddRange(l);
}
return list;
}
/// <summary>
/// 求數(shù)組中n個(gè)元素的組合
/// </summary>
/// <param name="t">所求數(shù)組</param>
/// <param name="n">元素個(gè)數(shù)</param>
/// <returns>數(shù)組中n個(gè)元素的組合的范型</returns>
public static List<T[]> GetCombination(T[] t, int n)
{
if (t.Length < n)
{
return null;
}
int[] temp = new int[n];
List<T[]> list = new List<T[]>();
GetCombination(ref list, t, t.Length, n, temp, n);
return list;
}
}
枚舉出鍵盤所有組合鍵代碼:
var combinePrefixKeyList = new Keys[] { Keys.Control, Keys.Alt, Keys.Shift };
var result = new List<List<Keys>>();
for(var i = ;i <= combinePrefixKeyList.Count() ;i++)
{
var combinationList = PermutationAndCombination<Keys>.GetCombination(combinePrefixKeyList, i);
var items = combinationList.Select(item => item.ToList()).ToList();
result.AddRange(items);
}
var keyNames = Enum.GetNames(typeof(Keys)).ToList();
foreach (var keyName in keyNames)
KeyList.Add(keyName, (Keys)Enum.Parse(typeof(Keys), keyName));
var combineKeyList = (from item in result
join key in KeyList.Select(k => k.Value).Except(new List<Keys> { Keys.None })
on true equals true
select item.Concat<Keys>(new List<Keys> { key })
).ToList();
Func<List<int>, int> GetCombinedKeyCode = null;
GetCombinedKeyCode = delegate(List<int> args)
{
if (args.Count == )
return ;
if (args.Count == )
return args[];
return args[] | GetCombinedKeyCode(args.Skip().ToList());
};
Func<int, int> GetVirtualKeyCode = delegate(int args)
{
return (byte)(args & 0xFF);
};
foreach (var item in combineKeyList)
{
var names = item.Select(key => Enum.GetName(typeof(Keys), key));
var keyCombination = string.Join("+", names);
Debug.Print(keyCombination + ":\t" + GetVirtualKeyCode(GetCombinedKeyCode(item.Cast<int>().ToList())).ToString());
}
總結(jié)
以上是生活随笔為你收集整理的c# 排列组合代码类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SICP 习题解 第二章
- 下一篇: 一、Signalr WebApi客服-数