c# 求第30位数的值
1,1,2,3,5,8,13,21,34,55....
求第30位数的值:
递归方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
还有没有其他方法,请评论?
为了明天能幸福,今天付出再多也不后悔。