Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 生成一系列递归字母(如Excel列标题)_C#_Recursion_F# - Fatal编程技术网

C# 生成一系列递归字母(如Excel列标题)

C# 生成一系列递归字母(如Excel列标题),c#,recursion,f#,C#,Recursion,F#,长话短说,我正在尝试创建某种国际象棋库,它可能适用于任何大小的国际象棋棋盘,int.MaxValuebyint.MaxValue。生成排名编号的无限列表非常简单: static IEnumerable<int> GetRankNumbers() { for(int i = 1; ; i++) yield return i; } 正如你所看到的,仅仅100件物品,它就会迅速膨胀。如何使其返回结果为a,b,c,…,z,aa

长话短说,我正在尝试创建某种国际象棋库,它可能适用于任何大小的国际象棋棋盘,
int.MaxValue
by
int.MaxValue
。生成排名编号的无限列表非常简单:

    static IEnumerable<int> GetRankNumbers()
    {
        for(int i = 1; ; i++)
            yield return i;
    }
正如你所看到的,仅仅100件物品,它就会迅速膨胀。如何使其返回结果为
a,b,c,…,z,aa,ab,ac,az,ba,bb。。etc

额外问题:我在F#中如何做到这一点


另外,我发现了一些类似的问题,但没有一个像我尝试的那样,用
IEnumerable
收益率
,以及理论上无限的集合来解决。最后一个是欺骗(仔细想想,我真的不需要更多……但我这样做是为了学习)。

首先,让我们构造一个函数,它将生成一个n字符组合序列。对于n=1(基本大小写),它将只生成一个单字符序列。对于n=2,它将生成2个字符的所有组合。等等

对于给定的
n
,如何生成
n
字符的所有组合?首先,我们在
n-1
字符的所有组合前加上“a”,然后在
n-1
字符的所有组合前加上“b”,依此类推:

let rec seqOfNChars n = 
  seq {
    for c in ['a' .. 'z'] do
      for s in seqOfNChars (n-1) do
        yield sprints "%c%s" c s
  }
然后添加基本情况:

let rec seqOfNChars n = 
  seq {
    for c in ['a' .. 'z'] do
      if n <= 1 then
        yield string c
      else
        for s in seqOfNChars (n-1) do
          yield sprintf "%c%s" c s
  }

首先,让我们构造一个函数,它将生成一系列n个字符的组合。对于n=1(基本大小写),它将只生成一个单字符序列。对于n=2,它将生成2个字符的所有组合。等等

对于给定的
n
,如何生成
n
字符的所有组合?首先,我们在
n-1
字符的所有组合前加上“a”,然后在
n-1
字符的所有组合前加上“b”,依此类推:

let rec seqOfNChars n = 
  seq {
    for c in ['a' .. 'z'] do
      for s in seqOfNChars (n-1) do
        yield sprints "%c%s" c s
  }
然后添加基本情况:

let rec seqOfNChars n = 
  seq {
    for c in ['a' .. 'z'] do
      if n <= 1 then
        yield string c
      else
        for s in seqOfNChars (n-1) do
          yield sprintf "%c%s" c s
  }

一种方法是递归地创建深度为X的所有列名,然后在每次迭代中按顺序只深入一个深度

这里有一个基本的解决方案,在C#中就是这样做的:

公共静态IEnumerable GetColumns()
{
int深度=1;
while(true)
{
foreach(GetColumns中的var列(深度))
收益返回列;
深度++;
}
}
私有静态IEnumerable GetColumns(整型深度)
{
字符串字母=“abcdefghijklmnopqrstuvxyz”;
foreach(字母形式的变量c)
如果(深度==1)
收益率收益率c.ToString();
其他的
{
foreach(GetColumns中的var sub(深度-1))
收益率c+sub;
}
}
你可以在你可以玩的地方看到这一点

请注意,我没有对此进行广泛测试,因此您应该检查边界情况,以确定它是否正确


还要注意,这将创建大量字符串垃圾。如果您想避免这种情况,您可能应该以不同的方式构建字符串。

一种方法是递归地创建深度为X的所有列名,然后在每次迭代中按顺序只深入一个深度

这里有一个基本的解决方案,在C#中就是这样做的:

公共静态IEnumerable GetColumns()
{
int深度=1;
while(true)
{
foreach(GetColumns中的var列(深度))
收益返回列;
深度++;
}
}
私有静态IEnumerable GetColumns(整型深度)
{
字符串字母=“abcdefghijklmnopqrstuvxyz”;
foreach(字母形式的变量c)
如果(深度==1)
收益率收益率c.ToString();
其他的
{
foreach(GetColumns中的var sub(深度-1))
收益率c+sub;
}
}
你可以在你可以玩的地方看到这一点

请注意,我没有对此进行广泛测试,因此您应该检查边界情况,以确定它是否正确


还要注意,这将创建大量字符串垃圾。如果要避免这种情况,您可能应该以不同的方式构建字符串。

无需递归将整数转换为另一个基数:

IEnumerable<string> GetFileLetters()
{
    const string theLetters = "abcdefghjklmnopqrstuvwxyz"; // there is no 'i'.

    for (int i = 1; ; i++)
    {
        var s = "";
        var c = i;

        while (c > 0)
        {
            var m = (c - 1) % theLetters.Length;
            s = theLetters[m] + s;
            c = (c - m) / theLetters.Length;
        }

        yield return s;
    }
}
IEnumerable GetFileLetters()
{
const string theLetters=“abcdefghjklmnopqrstuvxyz”;//没有“i”。
for(int i=1;i++)
{
var s=“”;
var c=i;
而(c>0)
{
var m=(c-1)%字母长度;
s=字母[m]+s;
c=(c-m)/字母长度;
}
收益率;
}
}

显然,可以(应该)通过记忆和字符串生成器进行优化,但概念仍然存在。

无需递归将整数转换为另一个基数:

IEnumerable<string> GetFileLetters()
{
    const string theLetters = "abcdefghjklmnopqrstuvwxyz"; // there is no 'i'.

    for (int i = 1; ; i++)
    {
        var s = "";
        var c = i;

        while (c > 0)
        {
            var m = (c - 1) % theLetters.Length;
            s = theLetters[m] + s;
            c = (c - m) / theLetters.Length;
        }

        yield return s;
    }
}
IEnumerable GetFileLetters()
{
const string theLetters=“abcdefghjklmnopqrstuvxyz”;//没有“i”。
for(int i=1;i++)
{
var s=“”;
var c=i;
而(c>0)
{
var m=(c-1)%字母长度;
s=字母[m]+s;
c=(c-m)/字母长度;
}
收益率;
}
}

显然,可以(应该)通过记忆和字符串生成器进行优化,但概念仍然存在。

展示您解决问题的尝试。提出具体的问题-几乎一对一地翻译。你说“像excel列标题”,然后生成aa、bb、cc等。你应该生成aa、ab、ac、ad。。。az、ba、bb、bc。。。bz,ca,cb,cc,cd。。。你不应该吗?检查我创建的这个,我认为它应该做你想要的。我考虑过这个变体:显示你解决问题的尝试。提出具体的问题-几乎一对一地翻译。你说“像excel列标题”,然后生成aa、bb、cc等。你应该生成aa、ab、ac、ad。。。az、ba、bb、bc。。。bz,ca,cb,cc,cd。。。你不应该吗?检查我创建的这个,我认为它应该做你想做的。我考虑过这个变体:这不也会生成类似
a
?(即中间的空格)是的,我测试过了。是的
IEnumerable<string> GetFileLetters()
{
    const string theLetters = "abcdefghjklmnopqrstuvwxyz"; // there is no 'i'.

    for (int i = 1; ; i++)
    {
        var s = "";
        var c = i;

        while (c > 0)
        {
            var m = (c - 1) % theLetters.Length;
            s = theLetters[m] + s;
            c = (c - m) / theLetters.Length;
        }

        yield return s;
    }
}