递归的算法求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); }
原文地址:https://www.cnblogs.com/heyangyi/p/8799007.html
时间: 2024-10-12 17:39:26