LeetCode263. Ugly Number 丑数判断
Posted 糖梦梦是女侠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode263. Ugly Number 丑数判断相关的知识,希望对你有一定的参考价值。
题目:
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5
. For example, 6, 8
are ugly while 14
is not ugly since it includes another prime factor 7
.
Note that 1
is typically treated as an ugly number.
理解:
给定一个正整数,判断这个正整数是否为丑数。
丑数的定义为:1为丑数,丑数可以表示为有限个2、 3、 5相乘。
代码:
class Solution
public:
bool isUgly(int num)
if(num<=0)
return 0;
int temp1=num%2;
int temp2=num%3;
int temp3=num%5;
while(temp1==0||temp2==0||temp3==0)
if(temp1==0)
num/=2;
else if(temp2==0)
num/=3;
else
num/=5;
temp1=num%2;
temp2=num%3;
temp3=num%5;
return num==1;
;
以上是关于LeetCode263. Ugly Number 丑数判断的主要内容,如果未能解决你的问题,请参考以下文章