当前位置: 移动技术网 > IT编程>脚本编程>Python > python 水仙花

python 水仙花

2019年01月16日  | 移动技术网IT编程  | 我要评论

大学生活规划,css教程,遭客户信息骂一年

#简单
def narcissus():
for n in range(100, 1000, 1):
a, b, c = n//100, (n//10)%10, (n%100)%10
if a ** 3 + b ** 3 + c ** 3 == n:
print(n)

#使用yield写法
def narcissus_yield():
m, n = 100, 999
while m <= n:
a, b, c = m // 100, (m // 10) % 10, (m % 100) % 10
if a ** 3 + b ** 3 + c ** 3 == m:
yield m
m = m + 1

#包含水仙花等。。。
def narcissus_all(min, max):
if type(min) != int or type(max) != int:
msg = "参数类型必须为int"
raise typeerror(msg)
if min is none or max is none:
msg = "参数值不能为none!!!"
raise valueerror(msg)
if max == 0 or max < min:
msg = "max不能为0或者max必须比min值大"
raise valueerror(msg)
length = len(str(max))
while min <= max:
temp_val, sum = min, 0
for n in range(length):
sum += (temp_val%10) ** length
temp_val //= 10
if min == sum:
yield min
min = min + 1

for n in narcissus_all(100000, 999999):
print(n)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网