首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

一个算法C

2012-01-20 
请教高手一个算法C#假设我现在有一个数组数组里有固定的一组数,现在我想获得所有的小于该数组长度的所有数

请教高手一个算法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]);


Console.WriteLine(strReturn);
nCount++;
rList = new ArrayList(arrInput.Count - 1);
rList.Add(strReturn);
for(int i = nIndex; i < arrInput.Count;i++)
{
if(i == nIndex)
{
continue;
}else
{
rList.Add(arrInput[i]);
}
}
FindList(rList);
}
}
}
}
}

26个也太多了,不知道要花多久,楼主可以试试5,6个字母
[解决办法]
class Program
{
static readonly int SIZE = 26;
static char[] alpha = new char[SIZE];
static bool[] flags = new bool[SIZE];

static void Main(string[] args)
{

for (int i = 0; i < SIZE; i++)
{
alpha[i] = (char)(97 + i);
flags[i] = false;
}

int end = 3;
for (int i = 1; i <= end; i++)
{
Stack <int> goes = new Stack <int> ();
DoMe(goes, i, 0);
}
Console.WriteLine( "total: " + count.ToString());
}
static int count = 0;
static void DoMe(Stack <int> goes, int len, int start)
{
if (goes.Count == len)
{
Array.ForEach(goes.ToArray(), delegate(int tmp) { Console.Write(alpha[tmp]); });
Console.WriteLine();
flags[goes.Pop()] = false;
count++;
return;
}
for (int i = start; i < alpha.Length; i++)
{
if (goes.Count < len && flags[i] == false)
{
goes.Push(i);
flags[i] = true;
DoMe(goes, len, 0);
}
}
if(goes.Count > 0)
flags[goes.Pop()] = false;
}
}

热点排行