您的位置:首页 > 编程语言 > C#

C# 排列组合取值

2015-06-16 10:05 447 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
static void Main(string[] args)
{
//List<string> list = new List<string>();
//for (int i = 0; i < 6; i++)
//{
//    list.Add(i.ToString());
//}

List<string> list = new List<string> { "0", "1", "2", "5", "8", "4", "12", "32" };

Combination.C(list, 3);
Console.WriteLine("---------------------");
//Console.Read();

Combination.A(list, 0, 3);
Console.WriteLine("---------------------");
Console.Read();

}
}

public abstract class Combination
{
public 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;
}

/// <summary>
/// 对数组进行组合操作,选取selectCount个元素进行组合
/// </summary>
/// <param name="lsArray">即将进行组合操作的数组</param>
/// <param name="selectCount">选取的元素的个数</param>
public static void C(List<string> lsArray, int selectCount)
{
int totolcount = lsArray.Count;
int[] currectselect = new int[selectCount];
int last = selectCount - 1;

for (int i = 0; i < selectCount; i++)
{
currectselect[i] = i;
}

while (true)
{
for (int i = 0; i < selectCount; i++)
{
Console.Write(" {0} ", lsArray[currectselect[i]]);
}
Console.WriteLine();

if (currectselect[last] < totolcount - 1)
{
currectselect[last]++;
}
else
{
int pos = last;
while (pos > 0 && currectselect[pos - 1] == currectselect[pos] - 1)
{
pos--;
}

if (pos == 0) return;
currectselect[pos - 1]++;
for (int i = pos; i < selectCount; i++)
{
currectselect[i] = currectselect[i - 1] + 1;
}
}
}
}

/// <summary>
/// 对数组进行全排列
/// </summary>
/// <param name="lsArray">要进行全排列的数组</param>
/// <param name="begin">进行全排列的开始下标</param>
/// <param name="end">进行全排列的结束下标</param>
public static void A(List<string> lsArray, int begin, int end)
{
if (begin == end)
{
for (int i = 0; i <= end; i++)
Console.Write(" {0} ", lsArray[i]);
Console.WriteLine();
}

for (int i = begin; i <= end; i++)
{
Swap(lsArray, begin, i);
A(lsArray, begin + 1, end);
Swap(lsArray, begin, i);
}
}

/// <summary>
/// 交换数组中的下标为x,y的值
/// </summary>
/// <param name="lsArray">该数组</param>
/// <param name="x"></param>
/// <param name="y"></param>
static void Swap(List<string> lsArray, int x, int y)
{
string t = lsArray[x];
lsArray[x] = lsArray[y];
lsArray[y] = t;
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: