字符组合 思路 很急 例如 a b c d2个字符要组合 ab ac ad ba bc bd ca cb cd da db dc 3个字符 组合 以此类推 怎么个思路 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/u/20090217/21/f41ed9f6-f929-451c-a5c9-80d2e408422a.html static void Main(string[] args) { List<string> retVal = Output(new char[] { 'a', 'b', 'c', 'd' }); foreach (string item in retVal) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("done"); Console.ReadLine(); } static List<string> Output(char[] array) { if (array == null) { throw new ArgumentNullException("array"); } if (array.Length == 0) { return null; } List<string> retVal = new List<string>(); if (array.Length == 1) { retVal.Add(array[0].ToString()); return retVal; } foreach (char first in array) { retVal.Add(first.ToString()); char[] subArray = RemoveItem(array, first); List<string> subRet = Output(subArray); for (int i = 0; i < subRet.Count; i++) { subRet[i] = first + subRet[i]; } retVal.AddRange(subRet); } return retVal; } static char[] RemoveItem(char[] array, char removeItem) { char[] retVal = new char[array.Length - 1]; int i = 0; foreach (char item in array) { if (item != removeItem) { retVal[i] = item; i++; } } return retVal; } static void Main(string[] args) { List<int> InPut = new List<int> { 1, 2,3 };//输入数组 List<int> Setting = new List<int>(); int Replicate =2;//个数 int iResult = 0; Loop(InPut, Setting, Replicate, ref iResult); Console.Read(); } static void Loop(List<int> InPut, List<int> Setting, int Replicate, ref int count) { if (Setting.Count() == Replicate)//all setting { count++; //打印结果 foreach (int cur in Setting) { Console.Write(cur.ToString() + " "); } Console.WriteLine(); } else { foreach (int haha in InPut) { int iCount = Setting.Count(item => item == haha); if (iCount ==0 ) { Setting.Add(haha); Loop(InPut, Setting, Replicate, ref count);//nested call Setting.RemoveAt(Setting.Count - 1);//remove the last element } } } } N个样本空间:List<string> strs,存储样本我建议用ascii做,这样效率会更高。以2个字符排列为例:for(int i=0;i<n;i++){ str=strs[i]; for (int j=1;j<n;j++) { result += str + (string)(str的ascii码+j) + ","; } for (int j=1;j<=i;j++) { result += str + (string)(str的ascii码-j) + ","; }}大概写的 挺简单的啊 string[] s1 = new string[] { "a", "b", "c", "d" }; string[] s2 = s1; for (int i = 0; i < s1.Length; i++) { for (int j = 0; j < s2.Length; j++) { if(s1[i]!=s2[j]) Console.Write(s1[i]+s2[j]); } Console.WriteLine(); } c#------------ string[] s1 = new string[] { "a", "b", "c", "d" }; string[] s2 = s1; string[] s3 = s1; for (int i = 0; i < s1.Length; i++) { for (int j = 0; j < s2.Length; j++) { for (int k = 0; k < s3.Length; k++) { if (s1[i] != s2[j] && s1[i] != s3[k] && s2[j] != s3[k]) Console.WriteLine(s1[i] + s2[j]+s3[k]); } } Console.WriteLine(); } Console.ReadKey(); 参考下面的内容,做了修改 static string[] m_Data = { "A", "B", "C", "D", "E" }; static void Main(string[] args) { Dictionary<string, int> dic = new Dictionary<string, int>(); for (int i = 0; i < m_Data.Length; i++) { Console.WriteLine(m_Data[i]);//如果不需要打印单元素的组合,将此句注释掉 dic.Add(m_Data[i], i); } GetString(dic); Console.ReadLine(); } static void GetString(Dictionary<string, int> dd) { Dictionary<string, int> dic = new Dictionary<string, int>(); foreach (KeyValuePair<string, int> kv in dd) { for (int i = 0; i < kv.Value; i++) { Console.WriteLine(kv.Key + m_Data[i]); //dic.Add(kv.Key + m_Data[i], i); } for (int i = kv.Value + 1; i < m_Data.Length; i++) { Console.WriteLine(kv.Key + m_Data[i]); dic.Add(kv.Key + m_Data[i], i); } } if (dic.Count > 0) GetString(dic); } 实现了ba ca cb da db dc的加入。 菜鸟提问,C#继承的问题。 Wince下,控件ListView的事件问题 急、急、急!!!!!!!! 初学者向您请教:用c#做的东西发布的时候要包装些什么东西才能在一个干净的客户机器上运行? winform中的路径一般是如何处理的 c#高级编程第11章反射例程的疑问? mdi多文档界面C#程序触发问题 如何取得int[] 我需要asp.net的空间和域名 当你需要做一个画图板,你觉得应该选用哪个控件作为绘图区? 请教一个自定义panel Treeview的用法
static void Main(string[] args)
{
List<string> retVal = Output(new char[] { 'a', 'b', 'c', 'd' }); foreach (string item in retVal)
{
Console.WriteLine(item);
} Console.WriteLine();
Console.WriteLine("done");
Console.ReadLine();
}
static List<string> Output(char[] array)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if (array.Length == 0)
{
return null;
} List<string> retVal = new List<string>();
if (array.Length == 1)
{
retVal.Add(array[0].ToString());
return retVal;
} foreach (char first in array)
{
retVal.Add(first.ToString()); char[] subArray = RemoveItem(array, first);
List<string> subRet = Output(subArray);
for (int i = 0; i < subRet.Count; i++)
{
subRet[i] = first + subRet[i];
} retVal.AddRange(subRet);
} return retVal;
} static char[] RemoveItem(char[] array, char removeItem)
{
char[] retVal = new char[array.Length - 1];
int i = 0;
foreach (char item in array)
{
if (item != removeItem)
{
retVal[i] = item;
i++;
}
}
return retVal;
}
static void Main(string[] args)
{
List<int> InPut = new List<int> { 1, 2,3 };//输入数组
List<int> Setting = new List<int>();
int Replicate =2;//个数
int iResult = 0;
Loop(InPut, Setting, Replicate, ref iResult);
Console.Read();
} static void Loop(List<int> InPut, List<int> Setting, int Replicate, ref int count)
{
if (Setting.Count() == Replicate)//all setting
{
count++;
//打印结果
foreach (int cur in Setting)
{
Console.Write(cur.ToString() + " ");
}
Console.WriteLine();
}
else
{
foreach (int haha in InPut)
{
int iCount = Setting.Count(item => item == haha);
if (iCount ==0 )
{
Setting.Add(haha);
Loop(InPut, Setting, Replicate, ref count);//nested call
Setting.RemoveAt(Setting.Count - 1);//remove the last element
}
}
}
}
以2个字符排列为例:
for(int i=0;i<n;i++)
{
str=strs[i];
for (int j=1;j<n;j++)
{
result += str + (string)(str的ascii码+j) + ",";
}
for (int j=1;j<=i;j++)
{
result += str + (string)(str的ascii码-j) + ",";
}}大概写的
string[] s1 = new string[] { "a", "b", "c", "d" };
string[] s2 = s1;
for (int i = 0; i < s1.Length; i++)
{
for (int j = 0; j < s2.Length; j++)
{
if(s1[i]!=s2[j])
Console.Write(s1[i]+s2[j]);
}
Console.WriteLine();
}
------------ string[] s1 = new string[] { "a", "b", "c", "d" };
string[] s2 = s1;
string[] s3 = s1;
for (int i = 0; i < s1.Length; i++)
{
for (int j = 0; j < s2.Length; j++)
{
for (int k = 0; k < s3.Length; k++)
{
if (s1[i] != s2[j] && s1[i] != s3[k] && s2[j] != s3[k])
Console.WriteLine(s1[i] + s2[j]+s3[k]);
}
}
Console.WriteLine();
}
Console.ReadKey();
{
Dictionary<string, int> dic = new Dictionary<string, int>();
for (int i = 0; i < m_Data.Length; i++)
{
Console.WriteLine(m_Data[i]);//如果不需要打印单元素的组合,将此句注释掉
dic.Add(m_Data[i], i);
}
GetString(dic);
Console.ReadLine();
} static void GetString(Dictionary<string, int> dd)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> kv in dd)
{
for (int i = 0; i < kv.Value; i++)
{
Console.WriteLine(kv.Key + m_Data[i]);
//dic.Add(kv.Key + m_Data[i], i);
}
for (int i = kv.Value + 1; i < m_Data.Length; i++)
{
Console.WriteLine(kv.Key + m_Data[i]);
dic.Add(kv.Key + m_Data[i], i);
}
}
if (dic.Count > 0) GetString(dic);
}