using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
int n = 5000;
long arr = Foo(n, 0, 1);
DateTime end = DateTime.Now;
TimeSpan span = new TimeSpan(end.Ticks - start.Ticks);
Console.WriteLine("第" + n + "位递归" + arr.ToString() + "费时:" + span.TotalSeconds.ToString());
start = DateTime.Now;
arr = Foo(n);
end = DateTime.Now;
span = new TimeSpan(end.Ticks - start.Ticks);
Console.WriteLine("第" + n + "位循环" + arr.ToString() + "费时:" + span.TotalSeconds.ToString());
Console.Read();
}
static int[] GetNumber()
{
int[] arr = new int[200];
for (int i = 0; i < 200; i++)
{
arr[i] = i + 1;
}
for (int i = 0; i < 100; i++)
{
int n = (new Random()).Next(0, 199);
int temp = arr[i];
arr[i] = arr[n];
arr[n] = temp;
}
int[] arr1 = new int[100];
for (int i = 0; i < 100; i++)
{
arr1[i] = arr[i];
}
return arr1;
}
/// <summary>
/// 递归方式
/// </summary>
/// <param name="n"></param>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
static long Foo(int n, long a, long b)
{
if (n == 1)
{
return b;
}
return Foo(n - 1, b, a + b);
}
/// <summary>
/// 循环方式
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
static long Foo(int n)
{
long a = 1;
long b = 1;
long c = 1;
for (int i = 3; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return c;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
int n = 5000;
long arr = Foo(n, 0, 1);
DateTime end = DateTime.Now;
TimeSpan span = new TimeSpan(end.Ticks - start.Ticks);
Console.WriteLine("第" + n + "位递归" + arr.ToString() + "费时:" + span.TotalSeconds.ToString());
start = DateTime.Now;
arr = Foo(n);
end = DateTime.Now;
span = new TimeSpan(end.Ticks - start.Ticks);
Console.WriteLine("第" + n + "位循环" + arr.ToString() + "费时:" + span.TotalSeconds.ToString());
Console.Read();
}
static int[] GetNumber()
{
int[] arr = new int[200];
for (int i = 0; i < 200; i++)
{
arr[i] = i + 1;
}
for (int i = 0; i < 100; i++)
{
int n = (new Random()).Next(0, 199);
int temp = arr[i];
arr[i] = arr[n];
arr[n] = temp;
}
int[] arr1 = new int[100];
for (int i = 0; i < 100; i++)
{
arr1[i] = arr[i];
}
return arr1;
}
/// <summary>
/// 递归方式
/// </summary>
/// <param name="n"></param>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
static long Foo(int n, long a, long b)
{
if (n == 1)
{
return b;
}
return Foo(n - 1, b, a + b);
}
/// <summary>
/// 循环方式
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
static long Foo(int n)
{
long a = 1;
long b = 1;
long c = 1;
for (int i = 3; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return c;
}
}
}