以下哪个程序段是使用递归函数实现1到100求和
A: def product1(num): product=1 for i in range(1,num+1): product=product *i return productprint(product1(10))
B: def sum1(num): sum2=0 for i in range(1,num+1): sum2+=i return sum2 print(sum1(100))
C: def product2(num): if num==1: return 1 else: return num *product2(num-1)print(product2(10))
D: def sum_a(num1): if num1==1: return 1 else: return num1+sum_a(num1-1) print(sum_a(100))
A: def product1(num): product=1 for i in range(1,num+1): product=product *i return productprint(product1(10))
B: def sum1(num): sum2=0 for i in range(1,num+1): sum2+=i return sum2 print(sum1(100))
C: def product2(num): if num==1: return 1 else: return num *product2(num-1)print(product2(10))
D: def sum_a(num1): if num1==1: return 1 else: return num1+sum_a(num1-1) print(sum_a(100))
举一反三
- 编写一个函数判断客户输入一个数字为1位、2位还是更多为,代码如下:请补全空下的代码num=int(input("输入一位或两位的数字:"))n='0'(1)n='1'(2)n='2'(3)d='>;2'print(n+"位数。”) A: 1: if num>;-10 and num<;10:2: eilf num >;-100 and num<;100:3: else: B: 1: if num>;10 and num<;-10:2: eilf num >;-100 and num<;100:3: else: C: 1: if num>;-10 and num<;10:2: eilf num <;-100 and num>;100:3: elif: D: 1: if num>;-100 and num<;100:2: eilf num >;-10 and num<;10:3: else:
- 请在下列程序中横线上填写适当的代码 #使用递归函数,输出斐波拉契数列的前20位 def func(num): if num==1 or num==2: return 1 else: return print("斐波拉契数列的前20位的数是:",end="") for i in range(1,21): print(func(i),end=" ")
- 下列程序的输出结果是() num = 27 count = 0 while num > 0: if num % 2 == 0: num /=2 elif num % 3 ==0: num /= 3 else: num -=1 count += 1 print (count)
- 下列程序的输出结果是:sum = 0 def sum(i1, i2): result = 0 for i in range(i1, i2 + 1): result += i return result print sum(1, 10)
- 完善程序段,下列函数用于确定一个给定字符串str的长度,【1】、【2】分别填写的是( )。 int strlen(char str[]) { int num=0; while(【1】) 【2】; return (num); } A: str[num]!='\0' B: str[num]=='\0' C: ++num D: --num