,输出所有的 4 位攻瑰花数。所谓 4 位玫瑰花数是指一个- 4 位数的各个数位上的数字的 4 次方的和恰好等于该数字本身,如 19+ 6++3++4+=1 634,1 634 就是一个-4 位 玫瑰花数。 [要求]ゃ (1)定义函数 i Rose(),判断 五 是否为 4 位玫瑰花数。 (2)调用 is Rose(n)函数对所有的 4 位数进行判断,如果是 4 位玖瑰花数,则将其添加 到列表 lst 中,最后输出列表 1st 中所有的 4 位玫瑰花数
时间: 2024-02-06 07:10:28 浏览: 26
以下是 Python 代码:
```python
def isRose(n):
digit_sum = 0
temp_n = n
while temp_n > 0:
digit = temp_n % 10
digit_sum += digit ** 4
temp_n //= 10
if digit_sum == n:
return True
else:
return False
lst = []
for i in range(1000, 10000):
if isRose(i):
lst.append(i)
print(lst)
```
输出结果为:
```
[1634, 8208, 9474]
```
其中,1634、8208 和 9474 都是 4 位玫瑰花数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)