我怎么能回归斐波那契系列?我应该使用IList<>?

时间:2010-12-30 16:48:06

标签: c#

public int Fibonacci(int x)
{        
    int prev = -1;
    int next = 1;
    for (int i = 0; i < x; i++)
    {
        int sum = prev + next;
        prev = next;
        next = sum;
        Console.WriteLine(sum); 
    }
    return sum; // plz teel me how can i return whole list ??
}

如何返回上述系列的整个输出?即如果x = 3然后是0 1 1 2那么我该如何归还呢?

8 个答案:

答案 0 :(得分:15)

试试这个:

public IEnumerable<int> Fibonacci(int x)
{
    int prev = -1;
    int next = 1;
    for (int i = 0; i < x; i++)
    {
       int sum = prev + next;
       prev = next;
       next = sum;
       yield return sum;
    }
}

答案 1 :(得分:4)

这将计算整个列表并在完成(急切评估)后返回:

public IEnumerable<int> Fibonacci(int x)
{

    IList<int> fibs = new List<int>()

    int prev = -1;
    int next = 1;
    for (int i = 0; i < x; i++)
    {
     int sum = prev + next;
        prev = next;
        next = sum;
        fibs.Add(sum); 
    }

    return fibs;
}

这将使用yield计算每个项目并根据需要返回(延迟评估):

public IEnumerable<int> Fibonacci(int x)
{
    int prev = -1;
    int next = 1;
    for (int i = 0; i < x; i++)
    {
     int sum = prev + next;
        prev = next;
        next = sum;
        yield return sum;
    }
}

答案 2 :(得分:0)

你返回一个......返回一个。这并不复杂。

public IList<int> Fibonacci(int x)
{
    List<int> result = new List<int>();

    int prev = -1;
    int next = 1;
    for (int i = 0; i < x; i++)
    {
     int sum = prev + next;
        prev = next;
        next = sum;
        result.Add(sum);
    }

    return result;
}

答案 3 :(得分:0)

如果你想像列表或所有数字的数组一样返回,你只需要这样的东西:

public List<int> Fibonacci(int x)
{   
    List<int> returnList = new List<int>();

    int prev = -1;
    int next = 1;
    for (int i = 0; i < x; i++)
    {
        int sum = prev + next;
        prev = next;
        next = sum;
        returnList.Add(sum);
    }
    return returnList; 
}

答案 4 :(得分:0)

我受到Andrew Hare懒惰加载fibonacci的启发,在http://www.ienablemuch.com/2010/09/fibonacci-using-sql.html之前将我的斐波那契重写为延迟加载方法:

using System;
using System.Collections.Generic;

using System.Linq;

namespace Fibonacci
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine("Sans list. Lazy load stuff:");
            int i = 0;


            foreach(int n in Fibonacci().Take(10))
            {
                ++i;
                Console.WriteLine("Loading {0} {1}", i, n);             
            }


            Console.WriteLine("\nPick the 20th fibonacci:");
            Console.WriteLine("\n20th fibonacci: {0}", 
                 Fibonacci().Skip(20 - 1).Take(1).Single());


            Console.WriteLine("\nEagerly load everything in list:");
            i = 0;      
            foreach(int n in Fibonacci().Take(10).ToList())
            {
                ++i;
                Console.Write("\nEager loading {0} {1}", i, n);
            }

        }



        static IEnumerable<int> Fibonacci()
        {
         int a = 0,  b = 1;

         for(;;)
         {
          Console.Write("Lazy");         
          yield return a;
          int n = a;
          a += b;
          b = n;          
         }


        }
    }//class

}

输出:

Sans list. Lazy load stuff:
LazyLoading 1 0
LazyLoading 2 1
LazyLoading 3 1
LazyLoading 4 2
LazyLoading 5 3
LazyLoading 6 5
LazyLoading 7 8
LazyLoading 8 13
LazyLoading 9 21
LazyLoading 10 34

Pick the 20th fibonacci:
LazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazyLazy
20th fibonacci: 4181

Eagerly load everything in list:
LazyLazyLazyLazyLazyLazyLazyLazyLazyLazy
Eager loading 1 0
Eager loading 2 1
Eager loading 3 1
Eager loading 4 2
Eager loading 5 3
Eager loading 6 5
Eager loading 7 8
Eager loading 8 13
Eager loading 9 21
Eager loading 10 34

至少这个解决方案与其他解决方案不同:-)这不是以-1开头,可以使用unsigned int代替int

答案 5 :(得分:0)

            int n = 15; 
           string r = string.Empty;
            int s=0, t=1,u;
            for (int i = 0; i < n; i++)
            {
                if (i == 0)
                {                  
                    r += Convert.ToString(s)+Convert.ToString(t);  
                }
                else
                {
                    u = s + t;
                    s = t;
                    t = u;
                    r += Convert.ToString(u);                                         
                }
            }
            MessageBox.Show(r);

答案 6 :(得分:0)

我应该说第一个答案就足够了,但是如果你有大数字,那么你将会溢出int。 而是尝试使用ulong;我们需要更新它以在prev上不使用-1,因为你不能使用底片。 相反,你可以试试这个:

 public static IEnumerable<ulong> Generate(ulong n)
    {
        if (n < 1) yield break;
        yield return 1;
        ulong prev = 0;
        ulong next = 1;

        for (ulong i = 1; i < n; i++)
        {
            ulong sum = prev + next;
            prev = next;
            next = sum;
            yield return sum;
        }
    }

其中第一个数字返回1,并且不止于此使得Fibonacci使用先前的计算返回ulong格式的正数。

答案 7 :(得分:0)

static IEnumerable<int> Fibonacci(int n)
    {
        int a = 0, b = 1;
        yield return a;
        yield return b;
        for (int i = 0; i < n; i++)
        {
            yield return a = a + b;
            yield return b = b + a;
        }
    }