递归的算法求1,1,2,3,5,8.......的第30位数是多少,然后求这些数的和.
static void Main(string[] args) { //递归的算法求1,1,2,3,5,8.......的第30位数是多少,然后求这些数的和. int n = int.Parse(Console.ReadLine()); int Number = GetNumber(n); int NumerSUM = GetNumberSUM(n); Console.WriteLine(Number); Console.WriteLine(NumerSUM); Console.Read(); } public static int GetNumber(int n) { if (n == 1 || n == 2) return 1; else return GetNumber(n - 1) + GetNumber(n - 2); } public static int GetNumberSUM(int n) { if (n == 1) return 1; else if (n == 2) return 2; else return GetNumberSUM(n - 1) + GetNumber(n); }
博客内容仅代表个人观点,如发现阐述有误,麻烦指正,谢谢!