返回斐波那契数列C#

时间:2018-11-02 14:13:24

标签: c# fibonacci

我需要制作一个返回斐波那契数列中第n个整数的方法,我编写(编辑)的代码不起作用,有人可以在我的for循环部分中指导我。我需要使用网络表单并将斐波那契数列返回到特定点。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

{
    public partial class Default : System.Web.UI.Page
    {
        int i, temp;

        public void Page_Load(object sender, EventArgs e)
        {

        }

        public int Fibonacci(int x)
        {
            if (x == 0)
            {
                return 1;
            }
            if (x == 1)
            {
                return 1;
            }
            else
            {
                return (Fibonacci(x - 2) + Fibonacci(x - 1));
            }

        }

        public void btSubmit_Click(object sender, EventArgs e)
        {
            // getting input from user
            int num = Convert.ToInt32(txtInput.Text);

            // logic for fibonacci series
            for (i = 0; i < num; i++)
            {
                lblResult.Text = Fibonacci(i).ToString();
            }

        }
    }

}

4 个答案:

答案 0 :(得分:3)

首先,我们通常假设

IEnumerable<Customer> customerList = new Customer[] 
{ 
     new Customer { Name = "test1", Id = 999 }, 
     new Customer { Name = "test2", Id = 915 }, 
     new Customer { Name = "test8", Id = 986 },
     new Customer { Name = "test9", Id = 988 },
     new Customer { Name = "test4", Id = 997 },
     new Customer { Name = "test5", Id = 920 },
};

int currentIndex = 3;   //want to get object Name = "test8", Id = 986

请参见https://oeis.org/A000045

如果您想要一个 serie ,让我们实现一个 serie (在 F(0) = 0, F(1) = 1, ... F(N) = F(N - 1) + F(N - 2) IEnumerable<T>的帮助下):

yield return

有了生成器,我们可以轻松地将 using System.Linq; ... //TODO: do you really want int as a return type? BigInteger seems to be a better choice public static IEnumerable<int> Fibonacci() { int n_2 = 1; // your rules; or start Fibonacci from 1st, not 0th item int n_1 = 1; yield return n_2; yield return n_1; while (true) { int n = n_2 + n_1; yield return n; n_2 = n_1; n_1 = n; } } 的前num个数字( Linq Fiboncacci)取为

Take

如果有 lblResult.Text = string.Join(", ", Fibonacci().Take(num)); ,我们会得到

num == 7

如果要使用单个项目- 1, 1, 2, 3, 5, 8, 13 (索引基于从零开始的):

ElementAt

答案 1 :(得分:0)

您的错误是您覆盖了文本,而不是添加到文本。将其更改为lblResult.Text += Fibonacci(i).ToString()以进行追加。

但是请注意,将大量文本从循环添加到GUI元素是有问题的。您需要承担大量读写GUI Elemetns的开销。如果仅对每个用户触发事件执行一次操作,则不会很重要,但是从循环中您会很快注意到它。

最好在后面的代码中构建序列,然后一次性分发。我什至写了一个示例代码来展示这些问题:

using System;
using System.Windows.Forms;

namespace UIWriteOverhead
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int[] getNumbers(int upperLimit)
        {
            int[] ReturnValue = new int[upperLimit];

            for (int i = 0; i < ReturnValue.Length; i++)
                ReturnValue[i] = i;

            return ReturnValue;
        }

        void printWithBuffer(int[] Values)
        {
            textBox1.Text = "";
            string buffer = "";

            foreach (int Number in Values)
                buffer += Number.ToString() + Environment.NewLine;
            textBox1.Text = buffer;
        }

        void printDirectly(int[] Values){
            textBox1.Text = "";

            foreach (int Number in Values)
                textBox1.Text += Number.ToString() + Environment.NewLine;
        }

        private void btnPrintBuffer_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Generating Numbers");
            int[] temp = getNumbers(10000);
            MessageBox.Show("Printing with buffer");
            printWithBuffer(temp);
            MessageBox.Show("Printing done");
        }

        private void btnPrintDirect_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Generating Numbers");
            int[] temp = getNumbers(1000);
            MessageBox.Show("Printing directly");
            printDirectly(temp);
            MessageBox.Show("Printing done");
        }
    }
}

正如另一位评论者所提到的,您可能也想使用某种形式的多任务处理。确实,我的第一个多任务学习经验是斐波那契/总理数检查器。这是很好的学习例子。

答案 2 :(得分:0)

您可以使用整数数组将斐波那契数字保留到n并返回第n个斐波那契数字:

purrr_0.2.5

然后您可以这样称呼它:

public int GetNthFibonacci_Ite(int n)  
{  
    int number = n - 1; //Need to decrement by 1 since we are starting from 0  
    int[] Fib = new int[number + 1];  
    Fib[0]= 0;  
    Fib[1]= 1;  
    for (int i = 2; i <= number;i++)  
    {  
        Fib[i] = Fib[i - 2] + Fib[i - 1];  
    }  
    return Fib[number];  
}

并重新计算第7个斐波那契数

答案 3 :(得分:0)

使用斐波那契方法:

    public int Fibonacci(int n)
    {
        int a = 0;
        int b = 1;

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

然后替换:

lblResult.Text = Fibonacci(i).ToString();

收件人:

lblResult.Text += Fibonacci(i).ToString();