【问题标题】:Fibonacci Sequence Error in C#C#中的斐波那契数列错误
【发布时间】:2013-11-30 04:31:45
【问题描述】:

我最近开始学习 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] );
            i += 1;
        }

        return terms[n];
    }

我对 C# 的理解很差,因为 Visual Studio 告诉我不能将“Concat”与 int[] 一起使用 - 我正在尝试将新值附加到数组中。任何帮助都会很棒。

【问题讨论】:

  • 你的函数类型应该是 int[] : int[] fibonacci(int n)
  • @T-D - OP 想要返回传入的 index 处的值。返回类型应该是int,而不是int[]
  • @Oded 是的,你是对的,应该是int,对不起。
  • 这是您可以在 C# 中用于斐波那契计算的最佳方法:ilyatereschuk.blogspot.com/2013/12/blog-post.html

标签: c# loops while-loop sequence fibonacci


【解决方案1】:

C# 中的数组是固定长度的。

如果您想使用可变长度集合,请改用强类型List&lt;T&gt;,它有一个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];
}

【讨论】:

  • 我如何将返回的值转换为字符串?
  • @galacticfan - .NET 中的 每个 对象都有一个 ToString() 方法...一些用于显示字符串的内置函数会自动调用它 (System.Console.Write ,例如,或String.Format)。
  • 我忘了添加 () 括号,因为它是一种方法,而不是属性。 将植物面对桌子
  • @galacticfan - I know the feeling
【解决方案2】:

您不能追加到数组。在 .Net 中,数组的大小是固定的,创建后无法调整大小。

相反,您应该使用List&lt;int&gt; 及其Add() method

【讨论】:

    【解决方案3】:

    例如,您可以使用 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];
        }
    

    【讨论】:

      【解决方案4】:

      您不能将项目添加到数组中,因为它具有固定长度。使用List&lt;int&gt; 代替数组

      【讨论】:

        【解决方案5】:

        我很惊讶没有人提到修复数组大小。

        好吧,也许我错过了什么,但你可以这样做:

        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;
        }
        

        它比使用列表的版本平均快 2.5 倍

        但通常没有免费午餐:缺点是您的内存消耗没有得到平滑:您需要为所有需要的内存预先付费。

        【讨论】:

          【解决方案6】:

          不要将值附加到数组中。数组具有静态大小,您无法在创建后调整它们的大小。 使用

           List<int> and its Add() method instead of array.
          

          这是斐波那契数列的解决方案。

          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];
          }
          

          也可以这样:

          class FibonacciSeries
          {
              static void Main(string[] args)
              {
                  Console.WriteLine("Enter a num till which you want fibonacci series : ");
                  int val = Convert.ToInt32(Console.ReadLine());
                  int num1, num2;
                  num1 = num2 = 1;
                  Console.WriteLine(num1);
                  if (val > num2)
                  {
                      while (num2 < val)
                      {
                          Console.WriteLine(num2);
                          num2 += num1;
                          num1 = num2 - num1;
                      }
                  }
          
                  Console.ReadLine();
              }
          }
          

          这里是你的数组格式的解决方案

          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;
           }
          

          【讨论】:

            【解决方案7】:

            我会将其作为递归而不是循环。

            private static int fibonacci(int fib)
                {
                    if (fib == 2 || fib == 1)
                    {
                        return 1;
                    }
                    else
                    {
                        return fibonacci(fib - 1) + fibonacci(fib - 2);
                    }
                }
            

            【讨论】:

              【解决方案8】:

              这是一种更有效的查找斐波那契数的方法。

                  public static IEnumerable<double> FibList(int n)
                  {
                      for (int i = 1; i <= n; i++)
                      {
                          yield return Math.Round(Fib(i));
                      }
                  }
              
                  public static double Fib(double n)
                  {
                      double golden = 1.61803398875;
              
                      return (n == 0 || n == 1) ? 1 : (Math.Pow(golden, n) - Math.Pow(-golden, -n))/Math.Sqrt(5);
                  }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-03-04
                • 1970-01-01
                • 2015-06-05
                相关资源
                最近更新 更多