例如,
aaa .. aaz .. aba .. abz .. aca .. acz .. azz .. baa .. baz .. bba .. bbz .. zzz
基本上,假设对二进制进行计数,但不是从0到1,而是从a到z。
我一直在尝试使它工作几个小时,但无济于事,公式变得非常复杂,我不确定是否有更简单的方法可以做到。
谢谢阅读。
编辑:我现在有这样的东西,但还不完全是这样,我不确定是否有更好的方法:
private IEnumerable<string> GetWordsOfLength(int length) { char letterA = 'a', letterZ = 'z'; StringBuilder currentLetters = new StringBuilder(new string(letterA, length)); StringBuilder endingLetters = new StringBuilder(new string(letterZ, length)); int currentIndex = length - 1; while (currentLetters.ToString() != endingLetters.ToString()) { yield return currentLetters.ToString(); for (int i = length - 1; i > 0; i--) { if (currentLetters[i] == letterZ) { for (int j = i; j < length; j++) { currentLetters[j] = letterA; } if (currentLetters[i - 1] != letterZ) { currentLetters[i - 1]++; } } else { currentLetters[i]++; break; } } } }
对于可变数量的字母组合,您可以执行以下操作:
var alphabet = "abcdefghijklmnopqrstuvwxyz"; var q = alphabet.Select(x => x.ToString()); int size = 4; for (int i = 0; i < size - 1; i++) q = q.SelectMany(x => alphabet, (x, y) => x + y); foreach (var item in q) Console.WriteLine(item);