💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
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. ~~~ public class Solution { public boolean isUgly(int num) { if(num<=0) return false; if(num==1) return true; while(num>=2 && num%2==0) num/=2; while(num>=3 && num%3==0) num/=3; while(num>=5 && num%5==0) num/=5; return num==1; } } “/=”属于复合赋值运算符中的一种,表示把左边的变量除于右边变量的值赋予右边的变量,例如:a/=b等价于a=a/b。同类的还有=(例如:a=b等价于a=a*b)、+=(例如:a+=b等价于a=a+b)、-=(例如:a-=b等价于a=a-b)、%=(例如:a%=b等价于a=a%b(这里的%符号是求余运算符,a%b表示求a除于b的余数))。或许你还会问为什么a/=b为什么不直接用a=a/b来表示,这是因为这样的表示方法可以让程序更加简洁。 ~~~