显示字符串中所有字符的排列组合。
2006-11-01 19:19
429 查看
我要得到的是所有字符的排列组合,那么,我每次从所有字符中取出一个字符。然后我去先得到剩下字符的所有的排列组合,那么最后在这些所有的排列组合前面加上我取出来的这个数,那不就是得到了所有以我取出的这个字符开头的所有组合序列么?
最后我把所有字符中的每一个字符都让它打一次开头,也就是都先取出来一次,那么不就是所有的组合都实现了么?
using System;
using System.Collections;
namespace MyNameSpace
{
class GenerateWords
{
public static void Main()
{
ArrayList res = Generate_Permutations("abc");
Console.WriteLine( "Count = " + res.Count);
for(int i=0;i<res.Count;i++)
{
Console.WriteLine(res[i]);
}
}
public static ArrayList Generate_Permutations(string word)
{
ArrayList result = new ArrayList();
if(word.Length == 1)
{
result.Add(word);
return result;
}
for(int i=0;i<word.Length;i++)
{
string shorter_word = word.Substring(0,i) + word.Substring(i+1,word.Length-i-1);
ArrayList shorter_Permutations = Generate_Permutations(shorter_word);
for(int j=0;j<shorter_Permutations.Count;j++)
{
string longer_word = word[i].ToString() + shorter_Permutations[j].ToString();
result.Add(longer_word);
}
}
return result;
}
}
另一种求排列组合,跟上面的要求不一样,不过经常有人问!
using System;
using System.Collections;
namespace AllCombinations
{
class GenerateWords
{
public static void Main()
{
ArrayList res = Generate_Permutations("abcd");
Console.WriteLine( "Count = " + res.Count);
for(int i=0;i<res.Count;i++)
{
Console.WriteLine(res[i]);
}
}
public static ArrayList Generate_Permutations(string word)
{
ArrayList result = new ArrayList();
result.Add(word[word.Length-1]);
if(word.Length <= 1)
return result;
for(int i=word.Length -2 ;i>=0;i--)
{
int count = result.Count;
for(int j=0;j<count;j++)
{
result.Add(word[i].ToString() + result[j].ToString());
}
result.Add(word[i].ToString());
}
return result;
}
}
}
// The results is :
Count = 15
d
cd
c
bd
bcd
bc
b
ad
acd
ac
abd
abcd
abc
ab
a
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=881284
最后我把所有字符中的每一个字符都让它打一次开头,也就是都先取出来一次,那么不就是所有的组合都实现了么?
using System;
using System.Collections;
namespace MyNameSpace
{
class GenerateWords
{
public static void Main()
{
ArrayList res = Generate_Permutations("abc");
Console.WriteLine( "Count = " + res.Count);
for(int i=0;i<res.Count;i++)
{
Console.WriteLine(res[i]);
}
}
public static ArrayList Generate_Permutations(string word)
{
ArrayList result = new ArrayList();
if(word.Length == 1)
{
result.Add(word);
return result;
}
for(int i=0;i<word.Length;i++)
{
string shorter_word = word.Substring(0,i) + word.Substring(i+1,word.Length-i-1);
ArrayList shorter_Permutations = Generate_Permutations(shorter_word);
for(int j=0;j<shorter_Permutations.Count;j++)
{
string longer_word = word[i].ToString() + shorter_Permutations[j].ToString();
result.Add(longer_word);
}
}
return result;
}
}
另一种求排列组合,跟上面的要求不一样,不过经常有人问!
using System;
using System.Collections;
namespace AllCombinations
{
class GenerateWords
{
public static void Main()
{
ArrayList res = Generate_Permutations("abcd");
Console.WriteLine( "Count = " + res.Count);
for(int i=0;i<res.Count;i++)
{
Console.WriteLine(res[i]);
}
}
public static ArrayList Generate_Permutations(string word)
{
ArrayList result = new ArrayList();
result.Add(word[word.Length-1]);
if(word.Length <= 1)
return result;
for(int i=word.Length -2 ;i>=0;i--)
{
int count = result.Count;
for(int j=0;j<count;j++)
{
result.Add(word[i].ToString() + result[j].ToString());
}
result.Add(word[i].ToString());
}
return result;
}
}
}
// The results is :
Count = 15
d
cd
c
bd
bcd
bc
b
ad
acd
ac
abd
abcd
abc
ab
a
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=881284
相关文章推荐
- 显示字符串中所有字符的排列组合。
- Swift - 排列组合之全排列 (输入一个字符串,输出该字符串包含的字符的所有组合)
- 输入一个字符串,打印出该字符串中字符的所有排列
- 输入一个字符串,打印出该字符串中字符的所有排列
- 输入一个字符串,要求输出字符串中字符所有的排列,例如输入"abc",得到"abc","acb","bca","bac","cab","cba"
- 《剑指Offer》面试题:按字典序打印出该字符串中字符的所有排列
- 输出字符串中字符的所有排列
- 传入一个字符串参数 获取这个字符串内所有字符的 不同排列情况
- 输出该字符串中字符的所有组合
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
- 写正确函数需要注意的地方:输入一个字符串,打印出该字符串中字符的所有排列
- C#查找字符串所有排列组合的方法
- 软件设计艺术大师基本功--输出字符串中各个字符的组合(不是排列)
- 输入一个字符串,输出该字符串中字符的所有组合
- 9.9递归和动态规划(五)——确定某字符串的所有排列组合
- 任意数字、字符序列,输出它们所有的排列组合
- 题目:输入一个字符串,输出该字符串中字符的所有组合
- 求一个字符串中所有字符的组合
- 有一个纯字符串,编写一段代码,列出其所有字符的大小写组合 如 :字符串“ji”,字母组合['ji','jI','Ji','JI'] 即每个字符的大小写组合
- 从文件读取字符串,并求出该字符串包含的字符集中所有字符的全部组合