题目
输入一个整数,判断其是否为丑数Ugly Number。(把只包含质因子2,3和5的数称作丑数。例如6、8都是丑数,但7、14不是,因为它们包含质因子7。 习惯上我们把1当做是第一个丑数。)
基本思路
使用bool()函数进行丑数判断
代码
#include<iostream>
using namespace std;
bool UglyNumber(int num){
while(num%2==0)
{
num/=2;
}
while(num%3==0)
{
num/=3;
}
while(num%5==0)
{
num/=5;
}
return num==1;
}
int main(){
int num;
cout << "请输入一个数:" << endl;
cin >> num;
if(UglyNumber(num)){
cout<<num<<"是丑数"<<endl;
}
else{
cout<<num<<"不是丑数"<<endl;
}
return 0;
}