美文网首页
计算阶乘

计算阶乘

作者: tingshuo123 | 来源:发表于2018-08-18 23:43 被阅读12次

递归

int fact(int n)
{
    if (n <= 1) {
        return 1;
    }
    return n * fact(n - 1);
}

非递归

int fact(int n)
{
    int result = 1;
    
    for (int i=1; i<=n; i++) {
        result *= i;
    }
    
    return result;
}

相关文章

网友评论

      本文标题:计算阶乘

      本文链接:https://www.haomeiwen.com/subject/nzuqiftx.html