判断丑数python_LintCode Python 简单级题目 517.丑数
題目描述:
寫一個程序來檢測一個整數是不是丑數。
丑數的定義是,只包含質因子?2, 3, 5?的正整數。比如 6, 8 就是丑數,但是 14 不是丑數以為他包含了質因子 7。
注意事項
可以認為?1?是一個特殊的丑數。
您在真實的面試中是否遇到過這個題?
Yes
樣例
給出 num =?8,返回?true。
給出 num =?14,返回?false。
題目分析:
循環取2/3/5的余數和商即可。
余=0,商=1時才為丑數。
源碼:
class Solution:
# @param {int} num an integer
# @return {boolean} true if num is an ugly number or false
def isUgly(self, num):
# Write your code here
if num == 1: return True
if num == 0: return False
t = [2,3,5]
i = 0
while True:
if i == 3:
return False
res = num%t[i]
cor = num/t[i]
if cor == 1 and res == 0:
return True
elif res == 0:
num = num/t[i]
continue
else:
i += 1
原文:http://www.cnblogs.com/bozhou/p/6956365.html
總結
以上是生活随笔為你收集整理的判断丑数python_LintCode Python 简单级题目 517.丑数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python迷宫小游戏大全_C课程设计迷
- 下一篇: python svm算法smo cifa