【题目】
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.
【具体代码如下】
bool isUgly(int num)
{
if(num<=0)return false;
if(num==1)return true;
int check=num;
bool flag=true;
while(flag==true)
{
int cs;
if(check%2==0)
cs=1;
else if(check%3==0)
cs=2;
else if(check%5==0)
cs=3;
else cs=0;
switch(cs){
case 1:check=check/2;break;
case 2:check=check/3;break;
case 3:check=check/5;break;
case 0:flag=false;return false;
}
if(check==1)return true;
}
return flag;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 18:44:33