排列3D的全排列组合

           上午看见有c写的全排列,我也想用C#来实现一下,买过彩票的朋友应该都熟悉3d吧,那么我简单实现一个3d彩票的全排列,3D是从0-9(可能是1-9)这些数字,然后从中选3个数,可以有全排列的买法,也有组合的买法,那么下面贴代码了(vs2008)。

代码
//-------------------- 全排列和组合排列
class B_Test
{

/// <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;

}

}

}

}

#region

/// <summary>

/// 返回组合值(思想是用currectselect记录取的lsArray的每个数对应数组的位置,先把currectselect最后一数加到isarry.cout,接着是倒数第二个加到小于倒数第一个,依次类推知道第0位)

/// </summary>

/// <param name="lsArray">即将进行组合操作的数组</param>

/// <param name="selectCount">选取的元素的个数</param>

public static List<string> D(List<string> lsArray, int selectCount)
{

List
<string> list_return=new List<string>();
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++)
{
list_return.Add(lsArray[currectselect[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) break;

currectselect[pos
- 1]++;

for (int i = pos; i < selectCount; i++)
{

currectselect[i]
= currectselect[i - 1] + 1;

}

}

}

return list_return;

}

/// <summary>

/// list 排列的数组的次序

/// </summary>
/// <param name="list">排列数组</param>
/// <param name="selectCount">全排列的个数</param>
public static void PN(Func<List<string>, int, List<string>> list,List<string> list_input, int selectCount)
{
List
<string> list_get = new List<string>();
list_get
= list(list_input,selectCount);
List
<string> sub_list = new List<string>();
foreach (string lis in list_get)
{
sub_list.Add(lis);
if(sub_list.Count==selectCount)
{
A(sub_list,
0,selectCount-1);
sub_list.Clear();
}
}
Console.Write(
"----------p/n--------");
Console.WriteLine();


}
#endregion
/// <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>

public static void Swap(List<string> lsArray, int x, int y)

{

string t = lsArray[x];

lsArray[x]
= lsArray[y];

lsArray[y]
= t;

}
}

对排列3进行操作:

代码



static void Main(string[] args)
{

//B_Test b_test = new B_Test();
List<string> list = new List<string>();
for (int i = 1; i < 10; i++)
{

list.Add(i.ToString());
}

// B_Test.PN(B_Test.D(list,3),list,3);
//从数组p中任意选n个数进行全排列p(n)即排列三
B_Test.PN((a, b) => B_Test.D(a,b), list, 3);
//排列3组合
B_Test.C(list, 3);

//Console.WriteLine("---------------------");

//数组前n个数的全排列(0,1,2)的全排列
B_Test.A(list, 0, 3);
Console.Read();
}

 

posted on 2010-03-19 13:44  MR_ke  阅读(4913)  评论(12编辑  收藏  举报

导航