斐波那契数列数列递归与非递归方式实现
2017-10-10 12:53
363 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 递归实现斐波那契数列
{
class Program
{
/*返回第n个斐波那契数列项的值*/
static int Fibonacci(int n) {
/*斐波那契数列的前两项都是1*/
if ((1 == n) || (2 == n))
{
return 1;
}
else {
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
static void Main(string[] args)
{
Console.Write("输入总项数:");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Fibonacci:");
for (int i = 1; i <= n; i++) {
Console.Write("{0}\t",Fibonacci(i));
/*输出五个换一行*/
if (i % 5 == 0)
Console.WriteLine();
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 非递归实现斐波那契数列
{
class Program
{
static void Main(string[] args)
{
//搞一个能存30个数的long类型数组
long[] a = new long[30];
a[0] = 1;
a[1] = 1;
for (int j = 2; j < 30; j++) {
a[j] = a[j - 1] + a[j - 2];
}
int count = 0;
foreach (int item in a) {
Console.Write("{0}\t",item);
count++;
if (item % 5 == 0) {
Console.WriteLine();
}
}
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 递归实现斐波那契数列
{
class Program
{
/*返回第n个斐波那契数列项的值*/
static int Fibonacci(int n) {
/*斐波那契数列的前两项都是1*/
if ((1 == n) || (2 == n))
{
return 1;
}
else {
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
static void Main(string[] args)
{
Console.Write("输入总项数:");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Fibonacci:");
for (int i = 1; i <= n; i++) {
Console.Write("{0}\t",Fibonacci(i));
/*输出五个换一行*/
if (i % 5 == 0)
Console.WriteLine();
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 非递归实现斐波那契数列
{
class Program
{
static void Main(string[] args)
{
//搞一个能存30个数的long类型数组
long[] a = new long[30];
a[0] = 1;
a[1] = 1;
for (int j = 2; j < 30; j++) {
a[j] = a[j - 1] + a[j - 2];
}
int count = 0;
foreach (int item in a) {
Console.Write("{0}\t",item);
count++;
if (item % 5 == 0) {
Console.WriteLine();
}
}
Console.ReadKey();
}
}
}
相关文章推荐
- JAVA递归与非递归实现斐波那契数列
- 递归与非递归方式判断二叉树是否对称&& 按行打印二叉树【java实现】
- 递归和循环方式实现斐波那契数列
- 解析分别用递归与循环的方式求斐波那契数列的实现方法
- 递归方式、迭代方式实现的斐波那契数列。
- 详解python使用递归、尾递归、循环三种方式实现斐波那契数列
- 斐波那契数列的两种实现方式(递归(大于O(n方))和迭代(O(n))----网易笔试2013录
- 二叉树的三种遍历方式的递归与非递归实现
- 不用递归方法实现斐波那契数列,输入数字n 将会输出该数列的第n个数字
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 不用递归方法实现斐波那契数列,输入数字n 将会输出该数列的第n个数字
- JAVA 递归与非递归斐波那契数列的实现
- fibonacii数列(斐波那契数列)的递归实现及循环实现
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 二叉树三种遍历方式的递归与非递归实现
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 斐波那契数列的递归与非递归实现
- C语言斐波那契数列的四种实现方式—递归,迭代,数组,队列
- 斐波那契数列的两种实现方式(递归(大于O(n方))和迭代(O(n))----网易笔试2013录
- 斐波那契数列 递归与非递归实现