Crossin的编程教室

标题: 求助关于“丑数” [打印本页]

作者: chchch0720    时间: 2018-3-14 20:51
标题: 求助关于“丑数”

题目
代码
* 报错
先生,range这里有没有什么办法产生无穷大的范围啊?
测试数据超出9999就不行了,但是9999用更大的数字替换之后也会报错
真情求助


作者: crossin先生    时间: 2018-3-15 17:19
你用while呀
作者: chchch0720    时间: 2018-3-15 19:07
crossin先生 发表于 2018-3-15 17:19
你用while呀

是无限循环嘛?可是又会报错Time limit exceeded
太蠢了求指点
作者: TED    时间: 2018-3-16 15:53
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)




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5