有这样一个数列,第n个数是第n-1个数和第n-2个数的和,求第x个数是多少?
编写函数:
int Add(int a)
{
if (a == 1 || a == 2)
return 1;
else
return (Add(a - 1) + Add(a - 2));
}
第X数是 Add(x);
////////////////////////
Test Demo
///////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
while(true)
{
Console.WriteLine("please enter a number:");
int temp = int.Parse(Console.ReadLine());
if (temp == 0)
{
Console.WriteLine("Press any key to exit...");
Console.Read();
return;
}
long result = Compute.Add(temp);
Console.WriteLine("The result is:" + result);
}
}
}
public class Compute
{
public static long Add(int a)
{
if (a == 1 || a == 2)
return 1;
else
return (Add(a - 1) + Add(a - 2));
}
}
}