判乘方
题目描述
我们说一个数a是b的乘方,如果a可以被b整除,并且a/b也是b的乘方。
编写一个函数is_power接收形参a和b,当a是b的乘方时返回True;反之,返回False。
输入
Please input a: 8
Please input b: 2
输出
True
样例输入
8
2
样例输出
Please input a: Please input b: True
题解
a = int(input("Please input a: "))
b = int(input("Please input b: "))
def is_power(a,b):
if a>b:
if a%b==0:
is_power(a/b,b)
else:
print("False")
elif a==b:
print("True")
else:
print("False")
is_power(a,b)
分析
从题干得知可采用递归的思想,且可知道由是否整除与两数的商是否能整除来判定是否为乘方。