C# 查找Tribonaci系列中的第n个元素
我发现这是一个很好的例子。我正试图用C来解决这个问题。我的尝试:C# 查找Tribonaci系列中的第n个元素,c#,C#,我发现这是一个很好的例子。我正试图用C来解决这个问题。我的尝试: private static int NTerm_Tribonacci(int term) { int a = 0; int b = 1; int c = 1; int result = 0; if (term == 1) return a; if (term == 2)
private static int NTerm_Tribonacci(int term)
{
int a = 0;
int b = 1;
int c = 1;
int result = 0;
if (term == 1) return a;
if (term == 2) return b;
if (term == 3) return c;
for (int i = 4; i <= term; i++)
{
a = a + b + c; if ((1 + 3 * i) % term == 0) { result = a; break; }
b = a + b + c; if ((2 * i + i - 1) % term == 0) { result = b; break; }
c = a + b + c; if ((3 * i) % term == 0) { result = c; break; }
}
return result;
}
private static int NTerm_tribonaci(内部术语)
{
int a=0;
int b=1;
int c=1;
int结果=0;
如果(项==1)返回a;
如果(项==2)返回b;
如果(项==3)返回c;
对于(inti=4;i试试这个:
private static int NTerm_Tribonacci(int term)
{
int a = 0;
int b = 1;
int c = 1;
int result = 0;
if (term == 0) result = a;
if (term == 1) result = b;
if (term == 2) result = c;
while(term > 2)
{
result = a + b + c;
a = b;
b = c;
c = result;
term--;
}
return result;
}
请注意,根据链接中的定义,我假设第一个术语为T0,而不是T1
试试这个:
private static int NTerm_Tribonacci(int term)
{
int a = 0;
int b = 1;
int c = 1;
int result = 0;
if (term == 0) result = a;
if (term == 1) result = b;
if (term == 2) result = c;
while(term > 2)
{
result = a + b + c;
a = b;
b = c;
c = result;
term--;
}
return result;
}
请注意,根据链接中的定义,我假设第一个术语为T0,而不是T1
试试这个:
private static int NTerm_Tribonacci(int term)
{
int a = 0;
int b = 1;
int c = 1;
int result = 0;
if (term == 0) result = a;
if (term == 1) result = b;
if (term == 2) result = c;
while(term > 2)
{
result = a + b + c;
a = b;
b = c;
c = result;
term--;
}
return result;
}
请注意,根据链接中的定义,我假设第一个术语为T0,而不是T1
试试这个:
private static int NTerm_Tribonacci(int term)
{
int a = 0;
int b = 1;
int c = 1;
int result = 0;
if (term == 0) result = a;
if (term == 1) result = b;
if (term == 2) result = c;
while(term > 2)
{
result = a + b + c;
a = b;
b = c;
c = result;
term--;
}
return result;
}
请注意,根据链接中的定义,我假设第一个术语为T0,而不是T1
我认为递归方式太适合这种情况:
例如:
using System.IO;
using System;
class Program
{
static void Main()
{
int a=4, b;
b=tribo(a);
Console.WriteLine(b);
}
static public int tribo(int n)
{
if(n==0) return 0;
if(n==1) return 1;
if(n==2) return 1;
return(tribo(n-1)+tribo(n-2)+tribo(n-3));
}
}
这就给出了系列01 1 2 4 7 13 24…我认为递归方式太适合这种情况:
例如:
using System.IO;
using System;
class Program
{
static void Main()
{
int a=4, b;
b=tribo(a);
Console.WriteLine(b);
}
static public int tribo(int n)
{
if(n==0) return 0;
if(n==1) return 1;
if(n==2) return 1;
return(tribo(n-1)+tribo(n-2)+tribo(n-3));
}
}
这就给出了系列01 1 2 4 7 13 24…我认为递归方式太适合这种情况:
例如:
using System.IO;
using System;
class Program
{
static void Main()
{
int a=4, b;
b=tribo(a);
Console.WriteLine(b);
}
static public int tribo(int n)
{
if(n==0) return 0;
if(n==1) return 1;
if(n==2) return 1;
return(tribo(n-1)+tribo(n-2)+tribo(n-3));
}
}
这就给出了系列01 1 2 4 7 13 24…我认为递归方式太适合这种情况:
例如:
using System.IO;
using System;
class Program
{
static void Main()
{
int a=4, b;
b=tribo(a);
Console.WriteLine(b);
}
static public int tribo(int n)
{
if(n==0) return 0;
if(n==1) return 1;
if(n==2) return 1;
return(tribo(n-1)+tribo(n-2)+tribo(n-3));
}
}
这就给了系列0 1 2 4 7 13 24…我喜欢解决这类问题的“LINQ方式”:
public IEnumerable<long> InfiniteTribonacciSequence()
{
long a = 0, b = 1, c = 1;
long nextTerm;
yield return a;
yield return b;
yield return c;
while (true)
{
nextTerm = a + b + c;
yield return nextTerm;
a = b;
b = c;
c = nextTerm;
}
}
public IEnumerable InfiniteTribution序列()
{
长a=0,b=1,c=1;
长NEXTERM;
收益率a;
收益率b;
收益率c;
while(true)
{
nexterm=a+b+c;
下一步收益率;
a=b;
b=c;
c=nexterm;
}
}
但是这必须小心使用,因为像Min()
这样的方法会变得很疯狂。但是你可以使用例如InfiniteTribonacciSequence.Take(5).Last()
来获得序列的第五个元素。我喜欢解决这类问题的“LINQ方法”:
public IEnumerable<long> InfiniteTribonacciSequence()
{
long a = 0, b = 1, c = 1;
long nextTerm;
yield return a;
yield return b;
yield return c;
while (true)
{
nextTerm = a + b + c;
yield return nextTerm;
a = b;
b = c;
c = nextTerm;
}
}
public IEnumerable InfiniteTribution序列()
{
长a=0,b=1,c=1;
长NEXTERM;
收益率a;
收益率b;
收益率c;
while(true)
{
nexterm=a+b+c;
下一步收益率;
a=b;
b=c;
c=nexterm;
}
}
但是这必须小心使用,因为像Min()
这样的方法会变得很疯狂。但是你可以使用例如InfiniteTribonacciSequence.Take(5).Last()
来获得序列的第五个元素。我喜欢解决这类问题的“LINQ方法”:
public IEnumerable<long> InfiniteTribonacciSequence()
{
long a = 0, b = 1, c = 1;
long nextTerm;
yield return a;
yield return b;
yield return c;
while (true)
{
nextTerm = a + b + c;
yield return nextTerm;
a = b;
b = c;
c = nextTerm;
}
}
public IEnumerable InfiniteTribution序列()
{
长a=0,b=1,c=1;
长NEXTERM;
收益率a;
收益率b;
收益率c;
while(true)
{
nexterm=a+b+c;
下一步收益率;
a=b;
b=c;
c=nexterm;
}
}
但是这必须小心使用,因为像Min()
这样的方法会变得很疯狂。但是你可以使用例如InfiniteTribonacciSequence.Take(5).Last()
来获得序列的第五个元素。我喜欢解决这类问题的“LINQ方法”:
public IEnumerable<long> InfiniteTribonacciSequence()
{
long a = 0, b = 1, c = 1;
long nextTerm;
yield return a;
yield return b;
yield return c;
while (true)
{
nextTerm = a + b + c;
yield return nextTerm;
a = b;
b = c;
c = nextTerm;
}
}
public IEnumerable InfiniteTribution序列()
{
长a=0,b=1,c=1;
长NEXTERM;
收益率a;
收益率b;
收益率c;
while(true)
{
nexterm=a+b+c;
下一步收益率;
a=b;
b=c;
c=nexterm;
}
}
但是这必须小心使用,因为像Min()
这样的方法会变得疯狂。但是你可以使用例如InfiniteTribonacciSequence.Take(5).Last()
来获得序列的第五个元素。确实如此。请检查我发布的演示链接。从定义来看:“n=0,1,2,…的前几个术语是0,1,1,2,4,7,13,24,44,81,149,”因此,如果我将第一个术语的索引称为零,那么第五个术语将是7而不是4。如果你真的想将第一个术语称为术语1而不是术语0,只需根据需要增加数字。确实如此。请检查我发布的演示链接。根据定义:”n=0,1,2,…的前几个术语是0,1,1,2,4,7,13,24,44,81,149,“所以如果我把第一个术语的索引称为零,那么第五个术语将是7而不是4。如果你真的想把第一个术语称为术语1而不是术语0,只需根据需要增加数字。确实如此。请检查我发布的演示链接。根据定义:”n=0,1,2,…的前几个术语是0,1,1,2,4,7,13,24,44,81,149,“所以如果我把第一个术语的索引称为零,那么第五个术语将是7而不是4。如果你真的想把第一个术语称为术语1而不是术语0,只需根据需要增加数字。确实如此。请检查我发布的演示链接。根据定义:”n=0,1,2,…的前几个术语是0,1,1,2,4,7,13,24,44,81,149,"因此,如果我把第一个项的索引称为0,那么第五个项将是7,而不是4。如果你真的想把第一个项称为第1项而不是第0项,只需根据需要增加数字。我认为递归解决方案在C#或Java这样的语言中不是最优的。请注意:对不起,这个词是合适的,但我认为即使是最优的也不是必需的在OP的指导下,只需一个工作代码就足够了。通常使用Fibonacci来显示简单递归解决方案的缺陷,并将记忆作为一个概念引入C#中递归解决方案的唯一真正问题是编译器在可能的地方不支持尾部调用优化,而支持常见的递归Fibonacci解决方案不管怎么说,尾巴不是递归的