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)
登录即可查看完整答案
我们收录了全球超50000道真实原题与详细解析,现在登录,立即获得答案。
类似问题
Tower of Hanoi Below is the program to solve the tower of Hanoi problem. The game is to move all the disks from the first rod 'A' to the last rod 'C' subject to the following constraints. Only one disk may be moved at a time. Each move consists of taking the topmost disk from one of the stacks and placing it on top of another stack or on an empty rod. No disk may be placed on top of a disk that is smaller than it. To move n discs from peg A to peg C: move n−1 discs from A to B. This leaves disc n alone on peg A move disc n from A to C move n−1 discs from B to C so they sit on disc n Fill in the blanks below. class Tower { static int move(int n, char source, char destination, char auxiliary, int i) { if (n > 0 ) { i = move(n - 1, ___a___, ___b___, ___c___, i); System.out.println(i+ ": Move disk "+n+" from " + source + " to " + destination ); i = move(n - 1, auxiliary, destination, source, i+1); } return i; } public static void main(String[] args) { int numberOfDisks = 4 ; move(numberOfDisks, 'A', 'C', 'B', 1 ) ; } } 1: ___a___ 2: ___b___ 3: ___c___
The function ExpPower given below receives two inputs, x and n, and should return is a real number and n is a non-negative integer. Note that the exponent of x in the expression is 3n. ExpPower(x, n) If n = 0, then Return(?) y := ExpPower(x, n - 1) Return( y3 ) What is the correct value for the algorithm to return if n=0 ?
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?
更多留学生实用工具
希望你的学习变得更简单
加入我们,立即解锁 海量真题 与 独家解析,让复习快人一步!