c# 排列组合代码类
/// <summary>
/// 排列组件算法类
/// </summary>
/// <typeparam name="T"></typeparam>
public class PermutationAndCombination<T>
{
/// <summary>
/// 交换两个变量
/// </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>
/// 递归算法求数组的组合(私有成员)
/// </summary>
/// <param name="list">返回的范型</param>
/// <param name="t">所求数组</param>
/// <param name="n">辅助变量</param>
/// <param name="m">辅助变量</param>
/// <param name="b">辅助数组</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">所求数组</param>
/// <param name="startIndex">起始标号</param>
/// <param name="endIndex">结束标号</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>
/// 求从起始标号到结束标号的排列,其余元素不变
/// </summary>
/// <param name="t">所求数组</param>
/// <param name="startIndex">起始标号</param>
/// <param name="endIndex">结束标号</param>
/// <returns>从起始标号到结束标号排列的范型</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>
/// 返回数组所有元素的全排列
/// </summary>
/// <param name="t">所求数组</param>
/// <returns>全排列的范型</returns>
public static List<T[]> GetPermutation(T[] t)
{
return GetPermutation(t, , t.Length - );
}
/// <summary>
/// 求数组中n个元素的排列
/// </summary>
/// <param name="t">所求数组</param>
/// <param name="n">元素个数</param>
/// <returns>数组中n个元素的排列</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>
/// 求数组中n个元素的组合
/// </summary>
/// <param name="t">所求数组</param>
/// <param name="n">元素个数</param>
/// <returns>数组中n个元素的组合的范型</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());
}
c# 排列组合代码类的更多相关文章
- php实现排列组合
php实现排列组合 一.总结 1.回溯:回溯的函数参数有些生疏了,记录递归的位置(pos或step),还要有东西(vis数组)来记录这个是否已经被访问 2.php全局变量的使用 :外部定义的普通变量, ...
- C#的排列组合类
C#的排列组合类 //-----------------------------------------------------------------------------//// 算法:排列组合 ...
- C#排列组合类
//----------------------------------------------------------------------------- // // 算法:排列组合类 // // ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(三)——笛卡尔积组合
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(二)——排列生成
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(一)—组合生成
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- HDU 4045 Machine scheduling (组合数学-斯特林数,组合数学-排列组合)
Machine scheduling Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- [总结]数论和组合计数类数学相关(定理&证明&板子)
0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...
- 数组排列组合问题——BACKTRACKING
BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...
随机推荐
- 在centOS7.2上编译gcc4.1.2
1.下载安装gcc4.1.2安装包 wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.1.2/gcc-4.1.2.tar.bz2 注:其他版本的安装包可以在上级目录寻找到. ...
- LeNet-5模型的keras实现
import keras from keras.models import Sequential from keras.layers import Input,Dense,Activation,Con ...
- zabbix_server: error while loading shared libraries: libperconaserverclient.so.18
zabbix_server: error while loading shared libraries: libperconaserverclient.so.18 [root@dba_test_002 ...
- feign请求写法
@FeignClient(value = "test", url = "${proxy.srvs.test:}") public interface ISubS ...
- java 集合之HashMap、Hashtable、LinkedHashMap、TreeMap
HashMap 实现了Map接口,线程不安全. 实现原理: HashMap由数组+链表组成,数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的. 如果通过hash定位到数组位置没有链表, ...
- 【洛谷P1919】A*B Problem升级版
题目大意:rt 题解:将长度为 N 的大整数看作是一个 N-1 次的多项式,利用 FFT 计算多项式的卷积即可. 代码如下 #include <bits/stdc++.h> using n ...
- 微信小程序-自制弹出框禁止页面上下滑动
弹出 fixed 弹窗后,在弹窗上滑动会导致下层的页面一起跟着滚动. 解决方法: 在弹出层加上 catchtouchmove 事件 两种方法:(在电脑上测试是没有用的,这是触摸事件.因此,需要在手机端 ...
- NOIP2016提高A组模拟中秋节9.15总结
这套题不算难但是比赛上萎掉了. 第一题数论, 当找到一个合适的数就直接处理答案,再用筛法将处理过的删掉. 比赛上没想到筛法,只拿了70分. 第二题二分答案,然后验证合法性就可以. 但是由于不能二分小数 ...
- TF-epoch、 iteration和batchsize区别(转载)
from http://www.cnblogs.com/qggg/p/6876942.html 转自 http://blog.csdn.net/sinat_30071459/article/detai ...
- CentOS8 中文输入法
CentOS8发布了,安装了下试试,结果发现中文输入法调不出来. 系统安装完成后,在系统[设置]的[Region&Language]里的[输入源]里可以添加汉语输入源,但是不能打中文字. 下面 ...