国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
斐波那契數(shù)列邏輯
建立斐波那契數(shù)列的各種方法
1.迭代方法
2.遞迴方法
3.使用陣列的斐波那契
如何求斐波那契數(shù)列的第N項?
方法一
方法2
結論
首頁 後端開發(fā) C#.Net教程 C# 中的斐波那契數(shù)列

C# 中的斐波那契數(shù)列

Sep 03, 2024 pm 03:34 PM
c# c# tutorial

C#中的斐波那契數(shù)列斐波那契數(shù)列是著名的數(shù)列數(shù)列之一。序列是 0, 1, 1, 2, 3, 5, 8…。斐波那契數(shù)列從零和一開始,下一個數(shù)字是前兩個數(shù)字的總和。據(jù)說斐波那契數(shù)列是由Leonardo Pisano Bigollo先生在13世紀創(chuàng)造的。斐波那契數(shù)列對於某些場景很有用。基本上它最初是用來解決兔子問題,即一對兔子出生的數(shù)量。斐波那契數(shù)列在其他問題中也很有用。

斐波那契數(shù)列邏輯

與斐波那契數(shù)列一樣,該數(shù)字是其前面兩個數(shù)字的總和。因此,如果我們有一個斐波那契數(shù)列,例如0, 1, 1, 2, 3, 5, 8, 13, 21…根據(jù)這個,下一個數(shù)字將是其前兩個數(shù)字的總和,例如13 和21。所以下一個數(shù)字是 13 +21=34。

這是產生斐波那契數(shù)列的邏輯

F(n)= F(n-1) +F(n-2)

其中 F(n) 是術語編號,F(xiàn)(n-1) +F(n-2) 是前面值的總和。

所以如果我們有系列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...

依邏輯F(n)= F(n-1) +F(n-2)

F(n)= 55+89

F(n)= 144

下一學期是 144。

建立斐波那契數(shù)列的各種方法

斐波那契數(shù)列可以透過多種方式產生。

1.迭代方法

這種方式是產生系列的最簡單的方法。

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceFibonacciDemo
{
classProgram
{
staticint Fibonacci(int n)
{
intfirstnumber = 0, secondnumber = 1, result = 0;
if (n == 0) return 0; //It will return the first number of the series
if (n == 1) return 1; // it will return ?the second number of the series
for (int i = 2; i<= n; i++)? // main processing starts from here
{
result = firstnumber + secondnumber;
firstnumber = secondnumber;
secondnumber = result;
}
return result;
}
staticvoid Main(string[] args)
{
Console.Write("Length of the Fibonacci Series: ");
int length = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i< length; i++)
{
Console.Write("{0} ", Fibonacci(i));
}
Console.ReadKey();
}
}
}

2.遞迴方法

這是解決這個問題的另一種方法。

方法一

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceFibonacciDemo
{
classProgram
{
staticint Fibonacci(int n)
{
intfirstnumber = 0, secondnumber = 1, result = 0;
if (n == 0) return 0; //it will return the first number of the series
if (n == 1) return 1; // it will return the second number of the series
return Fibonacci(n-1) + Fibonacci(n-2);
}
staticvoid Main(string[] args)
{
Console.Write("Length of the Fibonacci Series: ");
int length = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i< length; i++)
{
Console.Write("{0} ", Fibonacci(i));
}
Console.ReadKey();
}
}
}

方法2

using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FibonacciSeries
{
class Program
{
public static void Fibonacci
(
int firstnumber,
int secondnumber,
int count,
int length,
)
{
if (count <= length)
{
Console.Write("{0} ", firstnumber);
Fibonacci(secondnumber, firstnumber + secondnumber, count + 1, length);
}
}
public static void Main(string[] args)
{
Console.Write("Length of the Fibonacci Series: ");
int length = Convert.ToInt32(Console.ReadLine());
Fibonacci(0, 1, 1, length);
Console.ReadKey();
}
}
}

輸出:

C# 中的斐波那契數(shù)列

3.使用陣列的斐波那契

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Program
{
public static int[] Fibonacci(int number)
{
int[] a = new int[number];
a[0] = 0;
a[1] = 1;
for (int i = 2; i < number; i++)
{
a[i] = a[i - 2] + a[i - 1];
}
return a;
}
public static void Main(string[] args)
{
var b = Fibonacci(10);
foreach (var elements in b)
{
Console.WriteLine(elements);
}
}
}

輸出:

C# 中的斐波那契數(shù)列

如何求斐波那契數(shù)列的第N項?

方法如下

方法一

代碼:

using System;
namespace FibonacciSeries
{
class Program {
public static int NthTerm(int n)
{
if ((n == 0) || (n == 1))
{
return n;
}
else
{
return (NthTerm(n - 1) + NthTerm(n - 2));
}
}
public static void Main(string[] args)
{
Console.Write("Enter the nth term of the Fibonacci Series: ");
int number = Convert.ToInt32(Console.ReadLine());
number = number - 1;
Console.Write(NthTerm(number));
Console.ReadKey();
}
}
}

上面的程式碼是求斐波那契數(shù)列的第n項。例如,如果我們想要找出該系列中的第 12th 項,那麼結果將為 89。

方法2

(O(Log t) 時間)。

還有另一個遞歸公式可用來找出第 t 個斐波那契數(shù) 如果 t 是偶數(shù)則 = t/2:

F(t) = [2*F(k-1) + F(k)]*F(k)

如果 t 是奇數(shù),則 k = (t + 1)/2

F(t) = F(k)*F(k) + F(k-1)*F(k-1)

斐波那契矩陣

求行列式後,我們將得到 (-1)t = Ft+1Ft-1 – Ft2

FmFt + Fm-1Ft-1 = Fm+t-1

透過將 t = t+1,

FmFt+1 + Fm-1Ft = Fm+t

設 m = t

F2t-1 = Ft2 + Ft-12

F2t = (Ft-1 + Ft+1)Ft = (2Ft-1 + Ft)Ft

要得到公式,我們將執(zhí)行以下操作

如果 t 是偶數(shù),則 k = t/2

如果 t 是奇數(shù),則 k = (t+1)/2

所以透過對這些數(shù)字進行排序我們可以防止不斷使用STACK的記憶體空間。它給出的時間複雜度為 O(n)。遞歸演算法效率較低。

代碼:

int f(n) :
if( n==0 || n==1 )
return n;
else
return f(n-1) + f(n-2)

現(xiàn)在當上述演算法運行 n=4

fn(4)

f(3)???????????? f(2)

f(2)?? f(1)???? f(1)?? f(0)

f(1)? f(0)

所以它是一棵樹。為了計算f(4),我們需要計算f(3)和f(2)等等。對於較小的值4,f(2)計算兩次,f(1)計算三次。添加的數(shù)量將會大量增長。

有一個猜想,計算f(n)所需的加法次數(shù)為f(n+1) -1。

結論

這裡迭代方法始終是首選,因為它有更快的方法來解決此類問題。這裡我們將斐波那契數(shù)列的第一個和第二個數(shù)儲存在前一個數(shù)和前一個數(shù)中(這是兩個變數(shù)),並且我們使用當前數(shù)來儲存斐波那契數(shù)。

以上是C# 中的斐波那契數(shù)列的詳細內容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

本網(wǎng)站聲明
本文內容由網(wǎng)友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

C# 中的隨機數(shù)產生器 C# 中的隨機數(shù)產生器 Sep 03, 2024 pm 03:34 PM

C# 隨機數(shù)產生器指南。在這裡,我們討論隨機數(shù)產生器的工作原理、偽隨機數(shù)和安全數(shù)的概念。

C# 中的階乘 C# 中的階乘 Sep 03, 2024 pm 03:34 PM

C# 階乘指南。這裡我們討論 C# 中階乘的介紹以及不同的範例和程式碼實作。

c#多線程和異步的區(qū)別 c#多線程和異步的區(qū)別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區(qū)別在於,多線程同時執(zhí)行多個線程,而異步在不阻塞當前線程的情況下執(zhí)行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優(yōu)勢是提高計算性能,異步的優(yōu)勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

C# 中的質數(shù) C# 中的質數(shù) Sep 03, 2024 pm 03:35 PM

C# 質數(shù)指南。這裡我們討論c#中素數(shù)的介紹和範例以及程式碼實作。

C#與C:歷史,進化和未來前景 C#與C:歷史,進化和未來前景 Apr 19, 2025 am 12:07 AM

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發(fā)明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協(xié)程,未來將專注於性能和系統(tǒng)級編程。 2.C#由微軟在2000年發(fā)布,結合C 和Java的優(yōu)點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發(fā)者的生產力和雲(yún)計算。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在這裡,我們討論 C# 中模式的介紹和前 3 種類型,以及其範例和程式碼實作。

xml怎麼改格式 xml怎麼改格式 Apr 03, 2025 am 08:42 AM

可以採用多種方法修改 XML 格式:使用文本編輯器(如 Notepad )進行手工編輯;使用在線或桌面 XML 格式化工具(如 XMLbeautifier)進行自動格式化;使用 XML 轉換工具(如 XSLT)定義轉換規(guī)則;或者使用編程語言(如 Python)進行解析和操作。修改時需謹慎,並備份原始文件。

C# 中的回文 C# 中的回文 Sep 03, 2024 pm 03:34 PM

C# 回文指南。在這裡,我們討論 C# 中回文背後的介紹和邏輯,以及各種方法及其程式碼。

See all articles