【问题标题】:Fibonnachi sequence in c#c#中的斐波那契数列
【发布时间】:2014-10-11 10:00:13
【问题描述】:

我目前正在尝试制作一个程序来打印斐波那契数列的前 50 个项。 斐波那契数列 像这样。 0,1,1,2,3,5,8,13 第 N 项是前两项之和。所以在 上面的示例下一项将是 21,因为它将是前两项相加 (8+13)。

我的代码目前没有显示这个,有人可以帮我理解为什么吗?

static void Main(string[] args)
    {
        int[]   fibonnachi  = new int[50];
        fibonnachi[0] = 0;
        fibonnachi[1] = 1;
        int fib2ago = 0;
        int fib1ago = 1;
        for (int counter = 2; counter < 51; counter++)
        {
            fibonnachi[counter] = fibonnachi[fib2ago] + fibonnachi[fib1ago];
            Console.Write(fibonnachi[counter] + " ,");
            fib2ago++;
            fib1ago++;

        }
        Console.ReadLine();
    }

【问题讨论】:

  • 添加一个写行,因为结果可以被缓冲。
  • 另外,注意索引,int[50] 有元素 [0..49]

标签: c# arrays console-application sequence


【解决方案1】:

也许您不想要一个在 maxInt 中断并溢出的版本,它通过在相同的函数中计算和输出来混合关注点或使用某些函数 预先标注的数组来弄脏内存;)

所以这是一个有趣的小版本产生一个无限序列:

    public static IEnumerable<System.Numerics.BigInteger> Fibonacci()
    {
        System.Numerics.BigInteger current = 0, next = 1;
        while (true) 
        {
            yield return current;
            next = current + next;
            current = next - current; // isn't mutation ugly to read?
        }
    }

你可以像这样使用它:

    foreach (var i in Fibonacci().Take(10)) 
    {
        Console.Write("{0} ,", i);
    }

    > 0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34 ,

注意您可能会将Sytem.Numerics 引用为BigInteger,也许您必须考虑一下b1b2技巧 - 这只是因为我不想引入一个虚拟变量来记住b2 来更新b1 ;)

无耻广告:

当然,您可以使用递归以更易读的方式做到这一点:

public static IEnumerable<System.Numerics.BigInteger> 
    Fibonacci(System.Numerics.BigInteger current,
              System.Numerics.BigInteger next)
{
    yield return current;
    foreach(var n in Fibonacci(next, current+next))
        yield return n;
}

使用 C# 这可能会在一段时间后破坏你的记忆(我不知道编译器如何处理这里的递归循环) - 但无论如何这在 F# 中要自然得多:

let fibonacci =
    let rec create current next =
        seq {
            yield current
            yield! create next (current + next)
        }
    create 0I 1I

或者可能更惯用的

let fibonacci =
    (0I, 1I)
    |> Seq.unfold (fun (current, next) -> 
        Some (current, (next, current + next)) )

如果你想看到真正的好东西,看看这个:

The Fibonacci sequence - HaskellWiki :)

【讨论】:

  • “突变读起来不丑吗?”是的,int previous = current; current = next; next = previous + current; 怎么样
  • @weston 是的 - 但这有点违背了这里的目的;)(我有点想用递归给出一些非常相似的东西)
【解决方案2】:

代码中的问题是:

  • 您正在循环到 50,但数组中的最高索引是 49。
  • 您在循环中将counter 增加了两次。
  • 循环不显示前两个数字,因此您必须先执行此操作。

更新代码:

int[] fibonnachi = new int[50];
fibonnachi[0] = 0;
fibonnachi[1] = 1;
int fib2ago = 0;
int fib1ago = 1;
Console.Write(fibonnachi[0] + " ,");
Console.Write(fibonnachi[1] + " ,");
for (int counter = 2; counter < 50; counter++) {
  fibonnachi[counter] = fibonnachi[fib2ago] + fibonnachi[fib1ago];
  Console.Write(fibonnachi[counter] + " ,");
  fib2ago++;
  fib1ago++;
}

稍微清理一下代码,您可以只使用一个计数器,并通过检查何时开始计算新值将前两项合并到循环中:

int[] fibonnachi = new int[50];
fibonnachi[0] = 0;
fibonnachi[1] = 1;
for (int counter = 0; counter < 50; counter++) {
  if (counter >= 2) {
    fibonnachi[counter] = fibonnachi[counter - 2] + fibonnachi[counter - 1];
  }
  Console.Write(fibonnachi[counter] + " ,");
}

【讨论】:

    【解决方案3】:

    总体上更好的方法是这个:

        for (int i = 0; i < 51; i++)
        {
            fibonnachi[i+2] = fibonnachi[i] + fibonnachi[i+1];
            Console.Write(fibonnachi[i] + " ,");
        }
    

    【讨论】:

      【解决方案4】:

      Here 说明一切

      static void Main(string[] args)
      {
          Console.WriteLine("Please enter a number");
          int number = Convert.ToInt32(Console.ReadLine());
          Fibonacci(0, 1, 1, number);
      }   
      
      public static void Fibonacci(int a, int b, int counter, int number)
      {
          Console.WriteLine(a);
          if (counter < number) Fibonacci(b, a+b, counter+1, number);
      }
      

      【讨论】:

        【解决方案5】:

        使用下面的代码:

        回避:

              public static int CalculateFibonacci(int n)
               {
                  if(n == 0 || n == 1)
                   return n;
                  else
                  return ( CalculateFibonacci(n-1) + CalculateFibonacci(n-2) );
                }
        

        非递归:

        int a = 0;
        int b = 1;
        int c = 1;
        
        for (int i = 0; i < n; i++)
        {
            c = b + a;
            a = b;
            b = c;
        }
        return c;
        

        【讨论】:

        • 第一个是针对O(n) 问题的O(n*n) 实现。
        【解决方案6】:

        您需要在循环开始之前手动打印前两个数字。

                         Console.WriteLine(fibonnachi[0]);
                         Console.WriteLine(fibonnachi[1]);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-05
          • 1970-01-01
          • 2021-11-30
          • 2019-04-06
          • 2015-07-05
          相关资源
          最近更新 更多