题目: 打印出所有的”水仙花数”,所谓”水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个”水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------
File Name: python练习100——【013】所有水仙花数
Description: python practice
Author: zhangzx
Date: 2020-12-13
-------------------------------------------------------
Change Activity:
2020-02-19
-------------------------------------------------------
"""
# 解法一: 遍历
def shuixian():
shuixianhua = []
for i in range(100, 1000):
x = i//100
y = (i - x*100)//10
z = i - x*100 - y*10
if i == (x**3 + y**3 + z**3):
shuixianhua.append(i)
return shuixianhua
print(shuixian())
# 解法二:
def shuixian_1():
shuixianhua = []
for i in range(100, 1000):
x = i//100
y = i%100//10 # 取余数再整除
z = i%10
if i == (x**3 + y**3 + z**3):
shuixianhua.append(i)
return shuixianhua
print(shuixian_1())
---------------------------以下是输出-----------------------------
解法一:
[153, 370, 371, 407]
解法二:
[153, 370, 371, 407]