目前尚不清楚您如何获得所需的行数 - 因为您说过N+1
,但您的示例仅提供N
行数。
以下可用于生成这些单独的行中的每一行,并且Algo
可以修改该方法以n+1
通过将代码粘贴在 while 循环中、递减n
并在 上len
使用来生成行:AppendLine
StringBuilder
char[] allowedChars = Enumerable.Range('A', 26).Concat(Enumerable.Range('a', 26))
.Select(i => (char)i).ToArray();
[TestMethod]
public void Test()
{
Assert.AreEqual("A", Algo(0, 1));
Assert.AreEqual("AB", Algo(1, 2));
Assert.AreEqual("ACE", Algo(2, 3));
Assert.AreEqual("ADGJ", Algo(3, 4));
Assert.AreEqual("AEIMQ", Algo(4, 5));
}
public string Algo(int n, int len)
{
StringBuilder sb = new StringBuilder();
int nextCharIndex = 0;
for (int f = 0; f < len; f++)
{
sb.Append(allowedChars[nextCharIndex]);
//the `%`, or mod, here wraps around the next character back to upper case
nextCharIndex = (nextCharIndex + n) % allowedChars.Length;
}
return sb.ToString();
}