c# 求第30位數的值

1,1,2,3,5,8,13,21,34,55....ide

求第30位數的值:spa

遞歸方法:code

 1    class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //找規律:
 6             //1,1,2,3,5,8,13,21,34,55,......
 7             int num = 30;
 8             Console.WriteLine(GetNum(30));
 9             Console.ReadKey();
10         }
11         /// <summary>
12         /// 求第30位數的值
13         /// </summary>
14         /// <param name="i"></param>
15         /// <returns></returns>
16         private static int GetNum(int i)
17         {
18             if (i<=0)
19             {
20                 return 0;
21             }else if (i>0 && i<=2)
22             {
23                 return 1;
24             }
25             else
26             {
27                 return GetNum(i - 1) + GetNum(i - 2);
28             }
29         }
30 
31     }
View Code

還有沒有其餘方法,請評論?blog

相關文章
相關標籤/搜索