题目
Fal25_CS003C_74229 Ch. 5 Pre-Quiz (Remotely Proctored)
单项选择题
Which of the following code snippets returns the factorial of a given number? (Hint: Factorial of 5 = 5! = 1 * 2 * 3 * 4 * 5 = 120)
选项
A.def factorial(num) :
if(num == 1) :
return 1
else :
print(num * factorial(num - 1))
B.def factorial(num) :
if(num == 1) :
return 1
else :
return num * factorial(num)
C.def factorial(num) :
return num * factorial(num - 1)
D.def factorial(num) :
if(num == 1) :
return 1
else :
return num * factorial(num - 1)
查看解析
标准答案
Please login to view
思路分析
To begin, let's evaluate what a correct factorial function must do. It needs a base case that stops recursion (commonly when num reaches 1) and a recursive case that reduces the problem size by calling itself with num - 1, multiplying the result by the current num.
Option 1: The function computes the factorial but uses print instead of return in the recursive branch.......Login to view full explanation登录即可查看完整答案
我们收录了全球超50000道考试原题与详细解析,现在登录,立即获得答案。
类似问题
Which of the following code snippets returns the factorial of a given number? (Hint: Factorial of 5 = 5! = 1 * 2 * 3 * 4 * 5 = 120)
What is a base case in recursion?
What will this recursive function print when called with countdown(3)?
Which of the following situations might cause a recursive function to run infinitely?
更多留学生实用工具
希望你的学习变得更简单
加入我们,立即解锁 海量真题 与 独家解析,让复习快人一步!