|
发表于 2021-10-20 01:25:38
|
显示全部楼层
本帖最后由 jackz007 于 2021-10-20 09:35 编辑
if n % i == 0:
if n // i == 1: # 此条件与上一级条件相冲突,永远不可能成立,所以,这个条件块完全是废码,可以直接删除,但是,其 else 语句块中的语句会 100 % 得到执行,所以,应该得到保留。
- #coding:gbk
- def fn(d , n):
- for i in range(2 , n + 1):
- if n % i == 0:
- d . append(i) # 把找到的素数因子 i 添加到列表 d 中
- fn(d , n // i) # 从 n 中去除素数因子 i,递归,继续寻找下一个素数因子
- break # 已经找到了素数因子 i,所以,没有必要继续循环了。
- d = []
- n = int(input())
- fn(d , n)
- print('%d = ' % n , end = '')
- for k in range(len(d)):
- if k:
- print(' x ' , end = '')
- print(d[k] , end = '')
- print()
复制代码
运行实况:
- D:\00.Excise\Python>python x.py
- 99
- 99 = 3 x 3 x 11
- D:\00.Excise\Python>python x.py
- 6
- 6 = 2 x 3
- D:\00.Excise\Python>python x.py
- 113
- 113 = 113
- D:\00.Excise\Python>python x.py
- 124
- 124 = 2 x 2 x 31
- D:\00.Excise\Python>python x.py
- 256
- 256 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2
- D:\00.Excise\Python>
复制代码 |
|