递归
1、简单的递归算法
1 static void Main(string[] args) 2 { 3 Console.WriteLine(Fun1(5)); 4 Console.WriteLine(Fun2(5)); 5 } 6 7 /// <summary> 8 /// 2的幂次方 9 /// </summary> 10 /// <param name="i">幂</param> 11 /// <returns>返回值</returns> 12 private static int Fun1(int i) 13 { 14 if (i == 1) 15 { 16 return 2; 17 } 18 return Fun1(i - 1) * 2; 19 } 20 21 /// <summary> 22 /// 阶乘 23 /// </summary> 24 /// <param name="i">最高数</param> 25 /// <returns>返回值</returns> 26 private static int Fun2(int i) 27 { 28 if (i == 1) 29 { 30 return 1; 31 } 32 return Fun2(i - 1) * i; 33 }
结果: