带有前缀字的C#字母排列

时间:2012-12-06 21:27:10

标签: c# word permutation

如何使用前缀词进行2个字母的排列?

像这样:

NAMEaa NAMEab NAMEac NAMEad NAMEae NAMEaf ... ...

4 个答案:

答案 0 :(得分:2)

for (char c1 = 'a'; c1 <= 'z'; c1++)
{
    for (char c2 = 'a'; c2 <= 'z'; c2++)
    {
        Console.WriteLine("NAME" + c1 + c2);
    }
}
顺便说一句,那些不是permutations

答案 1 :(得分:1)

您可以使用LINQ轻松获得所需的结果:

string prefix = "NAME";
string alphabet = "abcdefghijklmnopqrstuvwxyz";

IEnumerable<string> words = from x in alphabet
                            from y in alphabet
                            select prefix + x + y;

答案 2 :(得分:0)

创建一个包含所有字母表的数组,然后将索引循环两次。

答案 3 :(得分:0)

这不是排列,即组合。

将所需字符放在字符串中:

string chars = "abcdefghijklmnopqrstuvwxyz";

可能的组合数量为:

int combinations = chars.Length * chars.Length;

获得特定组合(0到组合-1):

string str =
  "NAME" +
  chars.Substring(combination / chars.Length, 1) +
  chars.Substring(combination % chars.Length, 1);

要获得所有组合,只需循环遍历它们:

string chars = "abcdefghijklmnopqrstuvwxyz";
int combinations = chars.Length * chars.Length;
List<string> result = new List<string>();
for (int i = 0; i < combinations; i++) {
  result.Add(
    "NAME" +
    chars.Substring(combination / chars.Length, 1) +
    chars.Substring(combination % chars.Length, 1);
  );
}