C# C语言中的斐波那契序列错误#
我最近开始学习C#(学习了其他语言),我正在尝试创建一个函数,使用while循环生成第n个术语的斐波那契序列,然后返回第n个术语的值。 我目前的代码是:C# C语言中的斐波那契序列错误#,c#,loops,while-loop,sequence,fibonacci,C#,Loops,While Loop,Sequence,Fibonacci,我最近开始学习C#(学习了其他语言),我正在尝试创建一个函数,使用while循环生成第n个术语的斐波那契序列,然后返回第n个术语的值。 我目前的代码是: void fibonacci(int n) { int[] terms = { 0, 1 }; int i = 2; while (i<=n) { terms.Concat( terms[i-1] + terms[i-2] );
void fibonacci(int n)
{
int[] terms = { 0, 1 };
int i = 2;
while (i<=n)
{
terms.Concat( terms[i-1] + terms[i-2] );
i += 1;
}
return terms[n];
}
void fibonacci(int n)
{
int[]项={0,1};
int i=2;
而(i不能附加到数组。在.Net中,数组的大小是恒定的,创建后不能调整大小
相反,您应该使用及其。不能附加到数组中。在.Net中,数组的大小是恒定的,创建后不能调整大小
相反,您应该使用及其。数组在C#中是固定长度的
如果要使用可变长度集合,请改用强类型的列表
,该列表具有添加
方法:
int fibonacci(int n)
{
var terms = new List<int>{ 0, 1 };
int i = 2;
while (i<=n)
{
terms.Add( terms[i-1] + terms[i-2] );
i += 1;
}
return terms[n];
}
intfibonacci(intn)
{
var terms=新列表{0,1};
int i=2;
而C#中的(i数组是固定长度的
如果要使用可变长度集合,请改用强类型的列表
,该列表具有添加
方法:
int fibonacci(int n)
{
var terms = new List<int>{ 0, 1 };
int i = 2;
while (i<=n)
{
terms.Add( terms[i-1] + terms[i-2] );
i += 1;
}
return terms[n];
}
intfibonacci(intn)
{
var terms=新列表{0,1};
int i=2;
而(i例如,您可以使用list并将代码更改为:
int fibonacci(int n)
{
List<int> terms = new List<int> { 0, 1 };
int i = 2;
while (i<=n)
{
terms.Add(terms[i-1] + terms[i-2]);
i += 1;
}
return terms[n];
}
intfibonacci(intn)
{
列表项=新列表{0,1};
int i=2;
而(i例如,您可以使用list并将代码更改为:
int fibonacci(int n)
{
List<int> terms = new List<int> { 0, 1 };
int i = 2;
while (i<=n)
{
terms.Add(terms[i-1] + terms[i-2]);
i += 1;
}
return terms[n];
}
intfibonacci(intn)
{
列表项=新列表{0,1};
int i=2;
while(i由于数组具有固定长度,因此无法将项目添加到数组中。使用列表
而不是数组由于数组具有固定长度,因此无法将项目添加到数组中。使用列表
而不是数组时,不要将值附加到数组中。数组具有静态大小,创建后无法调整大小。
使用
List及其Add()方法,而不是数组。
这是斐波那契级数的解
int fibonacci(int n)
{
var terms = new List<int>{ 0, 1 };
int i = 2;
while (i<=n)
{
terms.Add( terms[i-1] + terms[i-2] );
i += 1;
}
return terms[n];
}
intfibonacci(intn)
{
var terms=新列表{0,1};
int i=2;
而(i num2)
{
while(num2
在您的数组格式中,这里是解决方案
public int[] FibonacciSeriesArray(int num)
{
int[] arr = new int[num+1];
arr[0] = 0;
arr[1] = 1;
for (int startnum = 2; startnum <= num; startnum++)
{
arr[startnum] = arr[startnum - 1] + arr[startnum - 2];
}
return arr;
}
public int[]FibonacciSeriesArray(int num)
{
int[]arr=新int[num+1];
arr[0]=0;
arr[1]=1;
对于(int startnum=2;startnum不向数组追加值。数组具有静态大小,创建后无法调整大小。
使用
List及其Add()方法,而不是数组。
这是斐波那契级数的解
int fibonacci(int n)
{
var terms = new List<int>{ 0, 1 };
int i = 2;
while (i<=n)
{
terms.Add( terms[i-1] + terms[i-2] );
i += 1;
}
return terms[n];
}
intfibonacci(intn)
{
var terms=新列表{0,1};
int i=2;
而(i num2)
{
while(num2
在您的数组格式中,这里是解决方案
public int[] FibonacciSeriesArray(int num)
{
int[] arr = new int[num+1];
arr[0] = 0;
arr[1] = 1;
for (int startnum = 2; startnum <= num; startnum++)
{
arr[startnum] = arr[startnum - 1] + arr[startnum - 2];
}
return arr;
}
public int[]FibonacciSeriesArray(int num)
{
int[]arr=新int[num+1];
arr[0]=0;
arr[1]=1;
对于(int startnum=2;startnum我很惊讶没有人提到修改数组大小
也许我遗漏了什么,但你可以:
int[] FibonacciArray(int n)
{
int[] F = new int[n+1];
F[0] = 0;
F[1] = 1;
for (int i = 2; i <= n; ++i)
{
F[i] = F[i - 1] + F[i - 2];
}
return F;
}
int[]FibonacciArray(int n)
{
int[]F=新的int[n+1];
F[0]=0;
F[1]=1;
对于(inti=2;i我很惊讶没有人提到修改数组大小
也许我遗漏了什么,但你可以:
int[] FibonacciArray(int n)
{
int[] F = new int[n+1];
F[0] = 0;
F[1] = 1;
for (int i = 2; i <= n; ++i)
{
F[i] = F[i - 1] + F[i - 2];
}
return F;
}
int[]FibonacciArray(int n)
{
int[]F=新的int[n+1];
F[0]=0;
F[1]=1;
对于(inti=2;i,这里有一种更有效的方法来查找fibonnaci数
公共静态IEnumerable FibList(int n)
{
对于(inti=1;i,这里有一种更有效的方法来查找fibonnaci数
公共静态IEnumerable FibList(int n)
{
对于(inti=1;i我将它作为一个递归,而不是一个循环
private static int fibonacci(int fib)
{
if (fib == 2 || fib == 1)
{
return 1;
}
else
{
return fibonacci(fib - 1) + fibonacci(fib - 2);
}
}
我会把它当作递归,而不是循环
private static int fibonacci(int fib)
{
if (fib == 2 || fib == 1)
{
return 1;
}
else
{
return fibonacci(fib - 1) + fibonacci(fib - 2);
}
}
函数类型应为int[]:int[]fibonacci(int n)@T-D-OP希望返回传入索引处的值。返回类型应为int
,而不是int[]
@Oded是的,你是对的,它应该是int
,我很抱歉。这是在C#中用于斐波那契计算的最佳方法:你的函数类型应该是int[]:int[]斐波那契(int n)@T-D-OP希望返回传入索引处的值。返回类型应该是int
,而不是int[]
@Oded是的,你是对的,它应该是int
,很抱歉。这是在C#中用于斐波那契计算的最佳方法:然后如何将返回值转换为字符串?@galacticfan-在.NET中的每个对象都有一个ToString()
method…一些用于显示字符串的内置函数将自动调用它(System.Console.Write
,例如,string.Format
)。我忘记添加()用括号括起来,因为它是一个方法,而不是一个属性。Face移植到desk中。@galacticfan-然后如何将返回的值转换为字符串?@galacticfan-在.NET中的每个对象都有一个ToString()
方法…一些用于显示字符串的内置函数将自动调用该方法(System.Console.Write
,例如,或String.Format
)。我忘记添加()括号,因为它是一个方法,而不是一个属性。Face将植物放入桌面。@galacticfan-