如何在Python中使用递归查找数字的幂?
以下程序接受用户的数字和索引。递归rpower()函数使用这两个作为参数。该函数将数字反复递归乘以返回幂。
示例
def rpower(num,idx):
if(idx==1):
return(num)
else:
return(num*rpower(num,idx-1))
base=int(input("Enter number: "))
exp=int(input("Enter index: "))
rpow=rpower(base,exp)
print("{} raised to {}: {}".format(base,exp,rpow))输出结果
这是一个示例运行-
Enter number: 10 Enter index: 3 10 raised to 3: 1000
热门推荐
10 新人结婚父母祝福语简短
11 新年的祝福语简短的
12 姐妹结婚红包简短祝福语
13 送师姐的祝福语简短
14 广东考试祝福语朋友简短
15 高考祝福语 简短12字
16 老师祝福语简短故事作文
17 毕业贺卡老师祝福语简短
18 老师简短祝福语有哪些