日期:2014-05-19  浏览次数:21004 次

请教高手一个算法C#
假设我现在有一个数组   数组里有固定的一组数,现在我想获得所有的小于该数组长度的所有数的排列组合值,该怎么写?
(例如:26个字母组成的数组,现在我想获得分别由1,2个一直到25个字母的所有组合,不同顺序也算。
2个:ab,bc,ba...yz,zy,...)


------解决方案--------------------
试试:
char[] CharArr ={ 'a ', 'b ', 'c ',... }; //26个字母
foreach (char c in CharArr)
{
string retStr = c.ToString();
foreach (char c1 in CharArr)
{
if (c != c1)
{
retStr += c1.ToString();
Response.Write(retStr); //输出,也可以做其他的处理
}
else
Response.Write(c1.ToString());//输出,也可以做其他的处理
}
}
------解决方案--------------------
http://www.codeproject.com/cs/algorithms/combinations.asp
------解决方案--------------------
加入ASP。NET C#群吧,群号是: 1873156
------解决方案--------------------
递归 处理子函数 完成从总长为n的串中输出长度为k的子串

(调用输出长度为k-1的子串 总长为n-1)
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace TestNumber
{
class Program
{
static int nCount = 0;
static void Main(string[] args)
{
ArrayList arr = new ArrayList();
arr.Add( "a ");
arr.Add( "b ");
arr.Add( "c ");
arr.Add( "d ");
arr.Add( "e ");
arr.Add( "f ");
arr.Add( "g ");
arr.Add( "h ");
arr.Add( "i ");
arr.Add( "j ");
arr.Add( "k ");
arr.Add( "l ");
arr.Add( "m ");
arr.Add( "n ");
arr.Add( "o ");
arr.Add( "p ");
arr.Add( "q ");
arr.Add( "r ");
arr.Add( "s ");
arr.Add( "t ");
arr.Add( "u ");
arr.Add( "v ");
arr.Add( "w ");
arr.Add( "x ");
arr.Add( "y ");
arr.Add( "z ");
foreach(object obj in arr)
{
Console.WriteLine(obj.ToString());
nCount++;
}

for(int i = 0; i < 24; i++)
{
FindList(arr);
arr.RemoveAt(0);
}

//FindList(arr);
Console.WriteLine( "Count : " + nCount);
Console.ReadLine();
}

static void FindList(ArrayList arrInput)
{
ArrayList rList;
if(arrInput.Count == 2)
{
string strReturn = string.Concat(arrInput[0], arrInput[1]);
Console.WriteLine(strReturn);
nCount++;
}else
{

for(int nIndex = 1; nIndex < arrInput.Count; nIndex++)
{
string strReturn = string.Concat(arrInput[0], arrInput[nIndex]);