题目大意:如果一个数的素数因子只有2,3,5,我们称它为丑数。让你判断一个数是不是丑数。
Problem:
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.
Idea:
Math
Solution:
C++
1 2 3 4 5 6 7 8 9 10 11 |
// Author: Huahua // Running time: 9 ms class Solution { public: bool isUgly(int num) { const vector<int> factors{2, 3, 5}; for (const int factor : factors) while (num && num % factor == 0) num /= factor; return num == 1; } }; |
Related Problems:
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment