python 水仙花
2024-09-26 22:46:52
#简单
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)
最新文章
- MySQL8:连接查询
- mysql 连接慢的问题(超过了1秒)
- 318. Maximum Product of Word Lengths
- 理解 OpenStack + Ceph (2):Ceph 的物理和逻辑结构 [Ceph Architecture]
- ffmpeg-20160726-bin.7z
- Reading and Writing CSV Files in C#
- git 常用
- 每日英语:A Whiff Of &#39;Welcome Home&#39;
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(1)
- 分分钟知道tiles框架是干嘛的
- javascript--自己用的插件
- 基于visual Studio2013解决算法导论之017查找第n小元素
- Oracle 11g sys,system 密码忘记设置解决办法
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
- 5.For loops
- tensorflow-TFRecord报错ValueError: Protocol message Feature has no ";feature"; field.
- SQL Server中如何定位Row Lock锁定哪一行数据
- vue-cli笔记
- python导出数据到excel
- 2018.08.28 集合堆栈机(模拟+STL)
热门文章
- 安装最新版的wampserver,可以兼容php5和php7
- 自动化构建工具grunt的学习
- Kettle api 二次开发之 日志的保存
- 截屏快捷键(windows)自带截屏,不需要安装任何插件
- 【oracle】关于创建表时用default指定默认值的坑
- HDU Ellipse(simpson积分)
- hdu_5187_zhx&#39;s contest
- 对DataSet,DataRow,DateTable转换成相应的模型
- 关于windows下安装mysql数据库出现中文乱码的问题
- 详解PreparedStatement