newResults.Add(str[pos].ToString()); if (pos != str.Length - 1) { foreach (string s in results) { newResults.Add(str[pos].ToString() + s);
} foreach (string s in results) { newResults.Add(s); }
} results.Clear(); results = newResults; if (pos > 0) { Show(str, results, pos - 1); } else if (pos == 0) { foreach (string s in results) { Console.WriteLine(s); } } } public static void Main(String[] args) { List<string> results = new List<string>(); string str = "ABCD"; PermutationCombination.Show(str, results, str.Length - 1); }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Collections; namespace 排列 { class 排列 { System.Collections.ArrayList a = new System.Collections.ArrayList(); public 排列(string 字符串) { 递归求子串(字符串); a.RemoveAt(a.IndexOf(字符串)); a.Sort(); //以下为循环移除相同的项! for (int i = 1; i < a.Count; i++) { if (a[i].Equals(a[i-1])) { a.RemoveAt(i); i--; } } PrintIndexAndValues(a); } public static void PrintIndexAndValues(IEnumerable myList) { int i = 0; foreach (Object obj in myList) Console.WriteLine("\t[{0}]:\t{1}", i++, obj); Console.WriteLine(); } //以下为递归方法,把C字符传所有少一个字符的子串全部加入ArrayList //以下递归方法的出口为,字符传的长度等于1 private void 递归求子串(string c) { if (c.Length == 1) { a.Add(c); } else { a.Add(c); for (int i = 0; i < c.Length; i++) { string c1 = c.Remove(i, 1); 递归求子串(c1); } } } } }
for (int i = 0; i < s.Length; i++)
{
for (int length = 1; length < s.Length-i+1; length++)
{
Console.WriteLine( s.Substring(i, length) );
}
}
排列组合是高中学的,时间太长都不记得了~
你说的对。
被楼主用子串误导了。string s = "ABCD";
int combination = 1 << s.Length;for (int i = 1; i < combination - 1; i++)
{
for (int j = 0; j < s.Length; j++)
{
if( (i >> j) % 2 == 1 )Console.Write( s[j] );
}
Console.WriteLine();
}
{
/// <summary>
/// 显示一个字符串的排列组合
///
/// 主要思路就是:譬如字符串"ABCD"
/// 新添 已有 结果
/// "D"
/// "C","CD" "D" "C","CD","D"
/// "B","BC","BCD", "BD" "C","CD","D" "B","BC","BCD","BD","C","CD","D"
/// "A","AB","ABC","ABCD","AC","ACD","AD" "B","BC","BCD","BD","C","CD","D" "A","AB","ABC","ABCD","AC","ACD","AD","B","BC","BCD","BD","C","CD","D"
/// </summary>
/// <param name="str">字符串</param>
/// <param name="results">存放组合的子串</param>
/// <param name="pos">添加字符串的位置</param>
public static void Show(string str, List<string> results, int pos)
{
if (string.IsNullOrEmpty(str))
return;
List<string> newResults = new List<string>();
newResults.Add(str[pos].ToString());
if (pos != str.Length - 1)
{
foreach (string s in results)
{
newResults.Add(str[pos].ToString() + s);
}
foreach (string s in results)
{
newResults.Add(s);
}
}
results.Clear();
results = newResults;
if (pos > 0)
{
Show(str, results, pos - 1);
}
else if (pos == 0)
{
foreach (string s in results)
{
Console.WriteLine(s);
}
} } public static void Main(String[] args)
{
List<string> results = new List<string>();
string str = "ABCD";
PermutationCombination.Show(str, results, str.Length - 1);
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
namespace 排列
{
class 排列
{
System.Collections.ArrayList a = new System.Collections.ArrayList();
public 排列(string 字符串)
{
递归求子串(字符串);
a.RemoveAt(a.IndexOf(字符串));
a.Sort();
//以下为循环移除相同的项!
for (int i = 1; i < a.Count; i++)
{
if (a[i].Equals(a[i-1]))
{
a.RemoveAt(i);
i--;
}
}
PrintIndexAndValues(a);
}
public static void PrintIndexAndValues(IEnumerable myList)
{
int i = 0;
foreach (Object obj in myList)
Console.WriteLine("\t[{0}]:\t{1}", i++, obj);
Console.WriteLine();
} //以下为递归方法,把C字符传所有少一个字符的子串全部加入ArrayList
//以下递归方法的出口为,字符传的长度等于1
private void 递归求子串(string c)
{
if (c.Length == 1)
{
a.Add(c);
}
else
{
a.Add(c);
for (int i = 0; i < c.Length; i++)
{
string c1 = c.Remove(i, 1);
递归求子串(c1);
}
}
}
}
}