在 C# 中递归打印斐波那契字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9828762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 10:48:27  来源:igfitidea点击:

Print a string of fibonacci recursively in C#

c#recursionfibonacci

提问by Marin

Can that be done with no while loops?

可以在没有 while 循环的情况下完成吗?

static void Main(string[] args)
{
    Console.WriteLine("Please enter a number");
    int number = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine(" #" + Fibonacci(number));
}

public static int Fibonacci(int number)
{
    if (number <= 1)
    {
        return 1;
    }
    else
    {
        return Fibonacci(number - 2) + Fibonacci(number - 1);
    }
}

I can't even add a Console.WriteLinein the body of base case since it gets executed [number] number of times; Not sure how to do this without loops...

我什至不能Console.WriteLine在 base case 的主体中添加 a ,因为它被执行了 [number] 次;不知道如何在没有循环的情况下做到这一点......

采纳答案by Gus

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

回答by Marin

I didn't find a way to do it closest way is it to combine both loops + recursion

我没有找到最接近的方法是将两个循环+递归结合起来

    static void Main(string[] args)
    { 
        Console.WriteLine("Please enter a number");
              int number = Convert.ToInt32(Console.ReadLine());
        for(int counter=0;counter<number;counter++)      
        Console.WriteLine(" \n" + Fibonacci(counter) );

    }

    public static int Fibonacci(int number)
    {

        if (number == 0)
            return 0;
        else if(number ==1)
          return 1;
        else
        {
         return Fibonacci(number - 2) + Fibonacci(number - 1);
        }

    }

回答by Maheswaran Shanmugam

namespace Algorithms
{
    class Program
    {
        static void Main(string[] args)
        {
            string fibResult = "";
            fibResult =  FibCal(10);
            Console.WriteLine(fibResult);
            Console.ReadLine();
        }

        public static string FibCal(int n)
        {
            string series = "";
            int k, f1, f2 , f = 0;
            f1 = f2 = 1;
            if (n < 2) 
                return n.ToString();
            else
                for (k = 0; k < n; k++)
                {
                    f = f1 + f2;
                    f2 = f1;
                    f1 = f;
                    series += f.ToString() + ",";
                }

            return series;
        }

    }
}

Hope this helps

希望这可以帮助

回答by Brent White

Using recursion in this fashion is a very bad idea. It will cause memory problems very quickly. I know you want to avoid using while/for loops, but an array is really the best way to go.

以这种方式使用递归是一个非常糟糕的主意。它会很快导致内存问题。我知道您想避免使用 while/for 循环,但数组确实是最好的方法。

回答by igal sidoy

public static int Fibonatchi(int position) {

    if(position == 0) {
        return 1;
    }
    if(position == 1) {
        return 1;
    } else {
        return Fibonatchi(position - 2) + Fibonatchi(position - 1);
    }
}

回答by Sivakishore Teru

Using LINQ

使用 LINQ

   public static void fibSeriesEx3()
    {
        List<int> lst = new List<int> { 0, 1 };
        for (int i = 0; i <= 10; i++)
        {
            int num = lst.Skip(i).Sum();
            lst.Add(num);

            foreach (int number in lst)
                Console.Write(number + " ");
            Console.WriteLine();
        }
    }

回答by Daniel Monera

That's a way to do it by returning a value into the main.

这是一种通过将一个值返回到 main 中来做到这一点的方法。

public static void Main() {

公共静态无效主(){

    Console.WriteLine("Introduce the number");
    int num = Convert.ToInt32(Console.ReadLine());

    int num1 = 1, num2 = 1, counter = num-2; 

//Take 2 out to match the list as first 2 numbers doesn't count in the function.

//取出 2 以匹配列表,因为前 2 个数字在函数中不计算在内。

    Console.WriteLine(Fibo(num1, num2, counter));
}

public static int Fibo(int num1, int num2, int counter)    {

    int temp = num1;

    if (counter <= 0)
        return num2;
    else
        return Fibo(num1 = num2, num2 += temp, counter-1);
}

回答by Fabio Maulo

public static class Golden
{
    public static IEnumerable<long> Fibonacci()
    {
        var a = 0L;
        var b = 1L;
        var s = 0L;
        yield return a;
        while (a < long.MaxValue - b)
        {
            yield return b;
            s = a + b;
            a = b;
            b = s;
        }
    }

    public static IEnumerable<long> FibonacciR()
    {
        IEnumerable<long> Fibo(long a, long b)
        {
            yield return a;
            if (a < long.MaxValue - b)
            {
                foreach (var v in Fibo(b, a + b))
                {
                    yield return v;
                }
            }
        }
        return Fibo(0, 1);
    }
}

回答by CB4

I realize this may be an old thread, but oh well I think this kinda question is good in its nature.

我意识到这可能是一个旧线程,但是哦,我认为这个问题本质上是好的。

Using while loop/or recursive way of doing is not the optimal way of doing as it takes a O(2^n) times. A better way to do this is using what is already in memory like below. This should take at most O(n) time.

使用 while 循环/或递归的方式不是最佳方式,因为它需要 O(2^n) 次。更好的方法是使用内存中已经存在的内容,如下所示。这最多需要 O(n) 时间。

Cheers!

干杯!

 static double fibDynamic(int n)
        {
            double[] array = new double[n];
            array[0] = array[1] = 1;

            for(int i = 2; i < n; i++)
            {
                array[i] = array[i - 1] + array[i - 2];
            }
            return array[n-1];
        }

回答by Yevhen_Radchenko

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static int Main(string[] args)
        {

            int n, i = 0, c;
            Console.WriteLine("Enter the number of terms:");
            n = Convert.ToInt16(Console.ReadLine());

            Console.WriteLine("Fibonacci series\n");

            for (c = 1; c <= n; c++)
            {
                int result = FibonacciFunction(i);
                Console.Write(result + " " );
                i++;
            }
            Console.WriteLine();
            return 0;
        }

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

    }
}