斐波那契数列(C#)

斐波那契数,亦称之为斐波那契数列(意大利语: Successione di Fibonacci),又称黄金分割数列、费波那西数列、费波拿契数、费氏数列,指的是这样一个数列:1、1、2、3、5、8、13、21、……在数学上,斐波那契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=Fn-1+Fn-2(n>=2,n∈N*),用文字来说,就是斐波那契数列由 0 和 1 开始,之后的斐波那契数列系数就由之前的两数相加。

解题如下:

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

"http://www.w3.org/1999/xhtml">
"server">
"Content-Type" content="text/html; charset=utf-8"/>
    


    
"form1" runat="server">
请输入n的值:"server" ID="sa"> "server" ID="suit" Text="斐波那契数列" OnClick="suit_Click"/> 结果:"server" ID="sa1">

Default.aspx.cs:

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
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void suit_Click(object sender, EventArgs e)
    {
        long m = Convert.ToInt64(sa.Text.Trim());
        sa1.Text=""+ Fibonacci(m)+"";
    } 
    //速度快
    public static long FbnqSort2(long num)
    {
        long ret = 0;
        long num1 = 1;
        long num2 = 1;
        if (num == 1 || num == 2)
        {
            ret = 1;
        }
        else if (num > 2)
        {

            for (int i = 0; i < num - 2; i++)
            {
                ret = num1 + num2;
                num1 = num2;
                num2 = ret;
            }
        }
        else
        {
            ret = 0;
        }
        return ret;
    }
    //速度极慢
    public static long fib(long n)
    {
        if (n == 1 || n == 2)
        {
            return 1;
        }
        else if (n > 2)
        {
            long a = fib(n - 1);
            long b = fib(n - 2);
            return a + b;
        }
        else
        {
            return 0;
        }
    }
    //速度快
    private static long F2(long number)
    {
        long a = 1, b = 1;
        if (number == 1 || number == 2)
        {
            return 1;
        }
        else if (number > 2)
        {
            for (int i = 3; i <= number; i++)
            {
                long c = a + b;
                b = a;
                a = c;
            }
            return a;
        }
        else
        {
            return 0;
        }
    }
    //速度快
    public static long Fibonacci(long n)
    {
        long f0 = 0;
        long f1 = 1;
        long f2 = 0;
        int t = 2;
        if (n < 0)
        {
            return 0;
        }
        else if (n == 0 || n == 1)
        {
            return n;
        }
        else
        {
            while (t <= n)
            {
                f2 = f0 + f1;
                f0 = f1;
                f1 = f2;
                t++;
            }
            return f2;
        }
    }
    //速度极慢
    public static long F1(long number)
    {
        if (number == 1 || number == 2)
        {
            return 1;
        }
        else if (number > 2)
        {
            return F1(number - 1) + F1(number - 2);
        }
        else
        {
            return 0;
        }
    }
}

运行结果:

转载于:https://www.cnblogs.com/tianranbai/p/10748895.html

你可能感兴趣的:(斐波那契数列(C#))