单选题 适中
#20465

对下面两个函数,说法【错误】的是?( ) int factorialA ( int n ) { if ( n <= 1 ) return 1 ; return…

对下面两个函数,说法【错误】的是?( )

int factorialA(int n) {
    if (n <= 1) return 1;
    return n * factorialA(n - 1);
}
int factorialB(int n) {
    if (n <= 1) return 1;
    int res = 1;
    for(int i=2; i<=n; i++)
        res *= i;
}
未登录也能判分,登录后才会记录
答案与解析
正确答案:D
解析

两个计算阶乘的函数 factorialA 和 factorialB。

factorialA采用递归的方法,factorialB 采用循环方式实现,二者的时间复杂度均为O(n)。