這篇文章主要介紹了C#如何實現(xiàn)斐波那契數(shù)列,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

益陽ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
什么是斐波那契數(shù)列?經(jīng)典數(shù)學問題之一;斐波那契數(shù)列,又稱黃金分割數(shù)列,指的是這樣一個數(shù)列:1、1、2、3、5、8、13、21、……想必看到這個數(shù)列大家很容易的就推算出來后面好幾項的值,那么到底有什么規(guī)律,簡單說,就是前兩項的和是第三項的值,用遞歸算法計第50位多少。
這個數(shù)列從第3項開始,每一項都等于前兩項之和。
斐波那契數(shù)列:{1,1,2,3,5,8,13,21...}
遞歸算法,耗時最長的算法,效率很低。
public static long CalcA(int n)
{
if (n <= 0) return 0;
if (n <= 2) return 1;
return checked(CalcA(n - 2) + CalcA(n - 1));
}通過循環(huán)來實現(xiàn)
public static long CalcB(int n)
{
if (n <= 0) return 0;
var a = 1L;
var b = 1L;
var result = 1L;
for (var i = 3; i <= n; i++)
{
result = checked(a + b);
a = b;
b = result;
}
return result;
}通過循環(huán)的改進寫法
public static long CalcC(int n)
{
if (n <= 0) return 0;
var a = 1L;
var b = 1L;
for (var i = 3; i <= n; i++)
{
b = checked(a + b);
a = b - a;
}
return b;
}通用公式法
/// <summary>
/// F(n)=(1/√5)*{[(1+√5)/2]^n - [(1-√5)/2]^n}
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public static long CalcD(int n)
{
if (n <= 0) return 0;
if (n <= 2) return 1; //加上,可減少運算。
var a = 1 / Math.Sqrt(5);
var b = Math.Pow((1 + Math.Sqrt(5)) / 2, n);
var c = Math.Pow((1 - Math.Sqrt(5)) / 2, n);
return checked((long)(a * (b - c)));
}其他方法
using System;
using System.Diagnostics;
namespace Fibonacci
{
class Program
{
static void Main(string[] args)
{
ulong result;
int number = 10;
Console.WriteLine("************* number={0} *************", number);
Stopwatch watch2 = new Stopwatch();
watch2.Start();
result = F1(number);
watch2.Stop();
Console.WriteLine("F1({0})=" + result + " 耗時:" + watch2.Elapsed, number);
Stopwatch watch3 = new Stopwatch();
watch3.Start();
result = F2(number);
watch3.Stop();
Console.WriteLine("F2({0})=" + result + " 耗時:" + watch3.Elapsed, number);
Stopwatch watch4 = new Stopwatch();
watch4.Start();
result = F3(number);
watch4.Stop();
Console.WriteLine("F3({0})=" + result + " 耗時:" + watch4.Elapsed, number);
Stopwatch watch5 = new Stopwatch();
watch5.Start();
double result4 = F4(number);
watch5.Stop();
Console.WriteLine("F4({0})=" + result4 + " 耗時:" + watch5.Elapsed, number);
Console.WriteLine();
Console.WriteLine("結(jié)束");
Console.ReadKey();
}
/// <summary>
/// 迭代法
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
private static ulong F1(int number)
{
if (number == 1 || number == 2)
{
return 1;
}
else
{
return F1(number - 1) + F1(number - 2);
}
}
/// <summary>
/// 直接法
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
private static ulong F2(int number)
{
ulong a = 1, b = 1;
if (number == 1 || number == 2)
{
return 1;
}
else
{
for (int i = 3; i <= number; i++)
{
ulong c = a + b;
b = a;
a = c;
}
return a;
}
}
/// <summary>
/// 矩陣法
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
static ulong F3(int n)
{
ulong[,] a = new ulong[2, 2] { { 1, 1 }, { 1, 0 } };
ulong[,] b = MatirxPower(a, n);
return b[1, 0];
}
#region F3
static ulong[,] MatirxPower(ulong[,] a, int n)
{
if (n == 1) { return a; }
else if (n == 2) { return MatirxMultiplication(a, a); }
else if (n % 2 == 0)
{
ulong[,] temp = MatirxPower(a, n / 2);
return MatirxMultiplication(temp, temp);
}
else
{
ulong[,] temp = MatirxPower(a, n / 2);
return MatirxMultiplication(MatirxMultiplication(temp, temp), a);
}
}
static ulong[,] MatirxMultiplication(ulong[,] a, ulong[,] b)
{
ulong[,] c = new ulong[2, 2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
}
return c;
}
#endregion
/// <summary>
/// 通項公式法
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
static double F4(int n)
{
double sqrt5 = Math.Sqrt(5);
return (1/sqrt5*(Math.Pow((1+sqrt5)/2,n)-Math.Pow((1-sqrt5)/2,n)));
}
}
}OK,就這些了。用的long類型來存儲結(jié)果,當n>92時會內(nèi)存溢出。
C#是一個簡單、通用、面向?qū)ο蟮木幊陶Z言,它由微軟Microsoft開發(fā),繼承了C和C++強大功能,并且去掉了一些它們的復雜特性,C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、優(yōu)雅的語法風格、創(chuàng)新的語言特性和便捷的面向組件編程從而成為.NET開發(fā)的首選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因為C#缺乏性能極高的應用程序所需要的關(guān)鍵功能。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C#如何實現(xiàn)斐波那契數(shù)列”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!
網(wǎng)站題目:C#如何實現(xiàn)斐波那契數(shù)列
文章出自:http://chinadenli.net/article48/isgshp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、軟件開發(fā)、微信公眾號、關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站、網(wǎng)站導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)