c#排列组合递归算法
来源:互联网 发布:足球关注软件 编辑:程序博客网 时间:2024/05/21 15:48
从n个字符串中取m个字符的所有组合(无放回抽样)
数学原理
Cn m=Cn-1m+Cn-1m-1
c#代码示例
using System;
using System.Collections.Generic;
using System.Text;
namespace Combination
{
class Program
{
static void Main(string[] args)
{
string[] Sample = new string[] { "0", "1", "2", "3", "4", "5","6","7","8","9","a","b","c","d","e","f","g" };
List<string> SampleList = new List<string>();
SampleList.AddRange(Sample);
List<string> result = getCombination(SampleList, 3);
Console.Read();
}
static List<string> getCombination(List<string> SampleList, int m)
{
if (m == 1)
{
return SampleList;
}
List<string> result = new List<string>();
if (SampleList.Count == m)
{
StringBuilder temp_sb = new StringBuilder();
foreach (string s in SampleList)
{
temp_sb.Append(s);
}
result.Add(temp_sb.ToString());
Console.WriteLine(temp_sb.ToString());
return result;
}
string temp_firstelement = SampleList[0];
SampleList.RemoveAt(0);
List<string> temp_samplist1 = new List<string>();
temp_samplist1.AddRange(SampleList);
List<string> temp_list1 = getCombination(temp_samplist1, m - 1);
foreach (string s in temp_list1)
{
result.Add(temp_firstelement + s);
Console.WriteLine(temp_firstelement + s);
}
List<string> temp_samplist2 = new List<string>();
temp_samplist2.AddRange(SampleList);
List<string> temp_list2 = getCombination(temp_samplist2, m);
result.AddRange(temp_list2);
return result;
}
}
}
SampleList的Count如果小于m的情况没有处理
- c#排列组合递归算法
- C#递归求排列组合
- 排列组合的递归算法
- 排列组合算法(非递归)
- 排列组合递归算法 java
- 排列组合算法(递归)1
- 排列组合算法-----使用递归
- c#排列组合算法
- C#实现排列组合算法
- 排列组合算法的递归实现
- java递归算法中的排列组合问题及排列组合去重
- 排列组合非递归算法的实现
- 【算法】递归求解几类排列组合问题
- 排列组合算法之三: 递归法
- 字符串排列组合问题&递归算法(1)
- 字符串排列组合问题&递归算法(2)
- 排列组合递归和非递归算法总结篇
- c# C(m,n) 排列组合算法
- 奇怪的totem+rmvb播放问题
- JavaScript遍历html元素属性
- 男人最碰不得的十大危险女人
- 本地化时不同语言选择时的缓存
- Oracle开放Oracle App Server与Spring Framework的集成代码
- c#排列组合递归算法
- 新公会第二天
- 读网070308
- 项目开发实践1
- 弱弱的问一个:CSDN的验证码是干什么用的???
- 使用XML的五种场合
- XML基本规则
- 什么是DTD
- 在Delphi中进行指纹仪的二次开发