/// <summary> /// 递归 /// </summary> /// <param name="n"></param> /// <returns></returns> static int factorial(int n) { if (n > 1) { return factorial(n - 1); } else if (n == 1) { return 1; } return 0; } /// <summary> /// 非递归 /// </summary> /// <param name="n"></param> /// <returns></returns> static int Factorial(int n) { Stack<Int32> st = new Stack<int>(); st.Push(n); int tmp, total = 1; while ((tmp = st.Peek()) != 1) { total *= tmp; st.Pop(); st.Push(tmp - 1); } return total; } }