[算法]阶乘

#include <iostream>
#include <cmath>

using namespace std;
//递归
//每个递归函数都必须有一个非递归初始值
//递归的第二式都是用较小自变量的函数值来表达较大自变量的函数值

//1.factorial 阶乘
//n! = 1 当 n=0
//n! = n(n-1);当n>0
//算法复杂度O(n)
static int factorial(int n)
{
    if (n == 1) return 1;

    return n * factorial(n - 1);
}
int main()
{
    for (int i=1;i<=100;i++)
    {
        cout <<"i="<<i<<":"<< factorial(13) << endl;
    }
    cout << "hello world" << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tailiang/p/11712603.html