菲波纳奇数字
using System;
using System.Collections.Generic;
namespace ConsoleApplication3
{
class Program
{
private static List<int> resultList = new List<int>() { 0, 1 };
static void Main(string[] args)
{
int num = 89;
//给出一个数,使用递归计算出该数前的菲波纳奇数字序列,并判断是否是菲波纳奇数字
try
{
TestMethod(num, resultList.Count - 1);
foreach (int item in resultList)
{
Console.Write(item + "\t");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
static void TestMethod(int num, int index)
{
if (null != resultList && resultList.Count >= 2)
{
if (num == 0)
return;
if (index > 0 && index <= resultList.Count)
{
int tmpSum = resultList[index - 1] + resultList[index];
if (tmpSum < num)
{
resultList.Add(tmpSum);
TestMethod(num, resultList.Count - 1);
}
else if (tmpSum == num)
resultList.Add(tmpSum);
else
throw new Exception("该数字不是菲波纳奇数字");
}
}
}
}
}
using System.Collections.Generic;
namespace ConsoleApplication3
{
class Program
{
private static List<int> resultList = new List<int>() { 0, 1 };
static void Main(string[] args)
{
int num = 89;
//给出一个数,使用递归计算出该数前的菲波纳奇数字序列,并判断是否是菲波纳奇数字
try
{
TestMethod(num, resultList.Count - 1);
foreach (int item in resultList)
{
Console.Write(item + "\t");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
static void TestMethod(int num, int index)
{
if (null != resultList && resultList.Count >= 2)
{
if (num == 0)
return;
if (index > 0 && index <= resultList.Count)
{
int tmpSum = resultList[index - 1] + resultList[index];
if (tmpSum < num)
{
resultList.Add(tmpSum);
TestMethod(num, resultList.Count - 1);
}
else if (tmpSum == num)
resultList.Add(tmpSum);
else
throw new Exception("该数字不是菲波纳奇数字");
}
}
}
}
}