回归斐波那契数列 c#

Returning Fibonacci series c#

我需要创建一个方法,使 return 成为斐波那契数列中的第 n 个整数,我编写(编辑)的代码不起作用,谁能在我的 for 循环部分指导我。我需要使用网络表单和 return 斐波那契数列到特定点。

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

        }
    }

}

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

但是请注意,将循环中的大量文本附加到 GUI 元素是一个有问题的操作。您会招致大量开销读取和写入 GUI 元素。如果每个用户触发事件只执行一次并不重要,但从循环中您会很快注意到它。

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

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

正如另一位评论者所提到的,您可能也想进行某种形式的多任务处理。事实上,我的第一个多任务学习体验是 Fibbonacci/Prime 数字检查器。他们是很好的学习例子。

您可以使用整数数组来保存斐波那契数直到 n 并返回第 n 个斐波那契数:

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

然后你可以这样称呼它:

GetNthFibonacci_Ite(7);

并返回第 7 个斐波那契数

使用斐波那契的方法:

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

首先,我们通常假设

   F(0) = 0,
   F(1) = 1,
   ...
   F(N) = F(N - 1) + F(N - 2)   

参见 https://oeis.org/A000045

如果你想要一个系列,让我们实现一个系列(借助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;
    }
  }

有了生成器,我们可以轻松地获取 numFiboncacci 个数字 (Linq Take):

  lblResult.Text = string.Join(", ", Fibonacci().Take(num));        

万一num == 7我们会得到

  1, 1, 2, 3, 5, 8, 13

如果你想要一个个人项目-ElementAt(索引是零基础):

  // 8
  lblResult.Text = Fibonacci().ElementAt(5).ToString();