打印n的m个乘积而不在Python中使用任何循环。
给定数字n,不使用任何循环就打印m乘以n。这里我们使用递归函数。
例子
Input: n = 15 Output: 15 10 5 0 5 10 15
算法
Step 1: Given n. Step 2: If we are moving back toward the n and we have reached there, then we are done. Step 3: If we are moving toward 0 or negative. Step 4: If m is greater, then 5, recursive function with true flag else recursive function is false. Step 5: If m is not greater than 5 then flag is false.
范例程式码
def printm(p, q, flag):
print(q)
if flag == False and p == q:
return
if flag:
if q - 5 > 0:
printm(p, q - 5, True)
else: # recur with false flag
printm(p, q - 5, False)
else: # If flag is false.
printm(p, q + 5, False)
# Driver Code
n = 15
printm(n, n, True)输出结果
15 10 5 0 5 10 15
热门推荐
10 军官男友结婚祝福语简短
11 订婚酒刻字祝福语简短
12 妈妈求婚祝福语大全简短
13 送老师新春祝福语简短
14 贺卡祝福语简短英文朋友
15 高考庆祝宴祝福语简短
16 大班女孩开学祝福语简短
17 祝福语同事励志语录简短
18 病人祝福语老师寄语简短