设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 4630|回复: 0
打印 上一主题 下一主题

求助关于“丑数”

[复制链接]

6

主题

2

好友

654

积分

实习版主

Rank: 7Rank: 7Rank: 7

楼主
发表于 2018-3-16 15:53:14 |显示全部楼层
chchch0720 发表于 2018-3-15 19:07
是无限循环嘛?可是又会报错Time limit exceeded
太蠢了求指点

def nthUglyNumber(n):
    list_chou=[]

    m=1
    while True:
        i = m
        while i%2==0:
            i = i/2
        while i%3==0:
            i = i/3
        while i%5==0:
            i = i/5
        if i==1:
            list_chou.append(m)
        m=m+1
        if len(list_chou)==n:
            return list_chou[n-1]

a = nthUglyNumber(197)
print(a)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-8 18:04 , Processed in 0.025708 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部