[转]组合算法(从几个数字中选几个然后组合)

组合算法,很不错,可以拿来选福彩双色球33选6,哈哈!
ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
private void button1_Click( object sender, EventArgs e)
{
int n = 33 ;
int m = 6 ;
string [] set = new string [n];
for ( int i = 0 ; i < n; i ++ )
{
set [i] = (i).ToString().PadLeft( 2 , ' 0 ' );
}
List
< string > result;

GC.Collect();

result
= GetCombinationF2( set , m); // n=20 about 44ms, n=40 about 6s
// result = GetCombinationF3(set, m); // n=20 about 16ms n=40 about 2.5s
// result = GetCombinationF4(set, m); // n=20 about 95ms n=40 about 13s

for ( int i = 0 ; i < 10 ; i ++ )
{
this .txtResult.Text += result[i] + " \r\n " ;
}
}

#region 组合算法
#region 组合算法2(非递归方法)
static List < string > GetCombinationF2( string [] strArray, int selectCount)
{
int totalCount = strArray.Length;
int [] currentSelect = new int [selectCount];
int last = selectCount - 1 ;
List
< string > output = new List < string > ();
string s;

// 付初始值
for ( int i = 0 ; i < selectCount; i ++ )
currentSelect[i]
= i;

while ( true )
{
s
= "" ;
// 输出部分,生成的时候从0计数,所以输出的时候+1
for ( int i = 0 ; i < selectCount; i ++ )
{
s
+= strArray[currentSelect[i]] + " \t " ;
}
output.Add(s);

// 如果不进位
if (currentSelect[last] < totalCount - 1 )
currentSelect[last]
++ ;
else
{
// 进位部分
int position = last;

while (position > 0 && currentSelect[position - 1 ] == currentSelect[position] - 1 )
position
-- ;

if (position == 0 )
break ;

currentSelect[position
- 1 ] ++ ;

for ( int i = position; i < selectCount; i ++ )
currentSelect[i]
= currentSelect[i - 1 ] + 1 ;
}
}
return output;
}
#endregion

#region 组合算法3
static List < string > GetCombinationF3( string [] data, int count)
{
Dictionary
< string , int > dic = new Dictionary < string , int > ();
List
< string > output = new List < string > ();
for ( int i = 0 ; i < data.Length; i ++ )
{
dic.Add(data[i], i);
}
SelectN(dic, data, count,
1 , ref output);
return output;
}

static void SelectN(Dictionary < string , int > dd, string [] data, int count, int times, ref List < string > output)
{
// 当前字典中的key与原始数据中值大于自身的每个key组合,比如第一轮递归中,01与02-33这些数组合,02与03-33这些数组合。递归五次,即可从33个数中选出6个。
Dictionary < string , int > dic = new Dictionary < string , int > ();

foreach (KeyValuePair < string , int > kv in dd)
{
for ( int i = kv.Value + 1 ; i < data.Length; i ++ )
{
if (times < count - 1 )
{
dic.Add(kv.Key
+ " \t " + data[i], i);
}
else
{
output.Add(kv.Key
+ " \t " + data[i]); // 不考虑输出,将此句注释掉
}
}
}
times
++ ;
if (dic.Count > 0 ) SelectN(dic, data, count, times, ref output);
}
#endregion

#region 组合算法4
static List < string > GetCombinationF4( string [] data, int count)
{
List
< string > output = new List < string > ();
int len = data.Length;
string start = " 1 " .PadRight(count, ' 1 ' ).PadRight(len, ' 0 ' );
string s;
while (start != string .Empty)
{
s
= "" ;
for ( int i = 0 ; i < len; i ++ )
if (start[i] == ' 1 ' ) s += data[i] + " \t " ;
output.Add(s);
start
= GetNext(start);
}
return output;

}

static string GetNext( string str)
{
string next = string .Empty;
int pos = str.IndexOf( " 10 " );
if (pos < 0 ) return next;
else if (pos == 0 ) return " 01 " + str.Substring( 2 );
else
{
int len = str.Length;
next
= str.Substring( 0 , pos).Replace( " 0 " , "" ).PadRight(pos, ' 0 ' ) + " 01 " ;
if (pos < len - 2 ) next += str.Substring(pos + 2 );
}
return next;
}
#endregion
#endregion

 

转载于:https://www.cnblogs.com/penglink/archive/2010/01/21/1653665.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一章 引论 1.1 组合数学研究的对象 1.2 组合问题典型实例 1.2.1 分派问题 1. 2.2 染色问题 1.2.3 幻方问题 1.2.4 36军官问题 1.2.5 中国邮路问题 习 题 第二章 排列与组合 2.1 两个基本计数原理 2.2 无重集的排列与组合 2.3 重集的排列与组合 2.4 排列生成算法 2.4.1 序数法 2.4.2 字典序法 2.4.3 轮法 2.5 组合生成算法 .2.6 应用举例 习 题 第三章 容斥原理 3.1 引 言 3.2 容斥原理 3.3 几个重要公式 3.4 错位排列 3.5 有限制的排列 3.6 棋阵多项式 3.7 禁位排列 习 题 第四章 鸽巢原理 4.1 鸽巢原理 4. 2 鸽巢原理的推广形式 4. 3 ramsey数 4.4 ramsey数的性质 4.5 ramsey定理 习 题 第五章 母函数 5.1 母函数概念 5.2 幂级数型母函数 5.3 整数的拆分 5.4 ferrers图 5.5 指数型母函数 习 题 第六章 递归关系 6.1 引言 6.2 几个典型的递归关系.. 6.3 用母函数方法求解递归关系 6.4 常系数线性齐次递归关系的求解 6.5 常系数线性非齐次递归关系的求解 6.6 非常系数非线性递归关系的求解 6.7 差分表法 6.8 stirling数 习 题 第七章 polya定理 7.1 有限集的映射 7.2 群的基本概念 7.3 置换群 7.4 置换的奇偶性 7.5 置换群下的共轭类 7.6 burnside引理 7.7 polya定理 7.8 polya定理的母函数型式 7.9 不标号图的计数 习 题 第八章 图论基础 8.1 图的基本概念 8.2 同构图、完全图与二分图 8.3 通路、回路与图的连通性 8.4 euler图与hamilton图 8.5 割集与树 8.6 图的矩阵表示法 8.7 平面图、对偶图与色数 8.8 匹配理论 8.9 网络流 习 题 第九章 拉丁方与区组设计 9.1 引言 9.2 拉丁方 9.3 有限域 9.4 正交拉丁方的构造 9.5 完全区组设计 9.6 平衡不完全区组设计(bibd) 9.7 区组设计的构造 9.8 steiner三连系 9.9 hadamard矩阵 习 题 第十章 线性规划 10.1 lp问题引例 10.2 lp问题的一般形式 10.3 lp问题的标准型 10.4 可行域和最优可行解 10.5 单纯形法 10.6 单纯形表格法 10.7 两阶段法 10.8 对偶原理 10.9 对偶单纯形法 10.10 应用举例 习 题 第十一章 组合优化算法与计算的时间复杂度理论 11.1 dijkstra算法 11.2 floyd算法 11.3 kruskal算法 11.4 求最优树的破圈法和统观法 11.5 二分图中最大匹配与最佳匹配的算法 11.6 fleury算法 11.7 中国邮路问题及其算法 11.8 深度优先搜索法--dfs算法 11.9 项目网络与关键路径法 11.10 网络最大流算法 11.11 状态移法 11.12 好算法、坏算法和np类问题 11.13 npc类问题 11.14 货郎问题的近似解 习 题... 参考文献

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值