263. 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.
所谓丑数,就是不能被2,3,5以外的其他素数整除的数。1,2,3,4,5,6,8,9,10,12,15是最前面的11个丑数。
题目大意:
编写程序判断一个给定的数字是否为“丑陋数” ugly number
丑陋数是指只包含质因子2, 3, 5的正整数。例如,6, 8是丑陋数而14不是,因为它包含额外的质因子7
注意,数字1也被视为丑陋数
结题方法:
让其重复的除2,3,5
C++代码:
1 class Solution { 2 public: 3 bool isUgly(int num) { 4 if(num<=0) return false; 5 vector<int> v{2,3,5}; 6 7 for(auto c:v) 8 { 9 while(num%c==0) num/=c; 10 } 11 return num==1; 12 } 13 };
时间: 2024-10-11 01:26:57