/// <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;
        }
    }

 

相关文章:

  • 2022-01-19
  • 2021-09-25
  • 2021-04-20
  • 2022-01-16
  • 2022-12-23
  • 2022-03-07
  • 2022-12-23
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
相关资源
相似解决方案