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.

Design: if a number not ugly, and it can be divided by 2/3/5. then the result still is a not ugly number.

class Solution {
public:
    bool isUgly(int num) {
        if(num < 1) return false;
        if(num < 7) return true;
        if(num % 2 == 0) return isUgly(num / 2);
        if(num % 3 == 0) return isUgly(num / 3);
        if(num % 5 == 0) return isUgly(num / 5);
        return false;
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,457评论 0 23
  • 我不会开船, 船靠在岸边。 昨夜的爱延伸到凌晨五点, 你亲吻了我的嘴唇, 送我一支口琴。 往回走的路上, 遇到暴风...
    锄风少年阅读 1,573评论 0 0
  • 感恩老公一早送我去报名点坐车,让我可以很快的到达集合点。谢谢谢谢谢谢! 感恩已经等在报名点的美女以及她老公,邀请我...
    轻风style阅读 1,215评论 0 0
  • 上周看了3本关于徐志摩的书籍,一本是徐志摩陆小曼的爱情传记;一本是《爱眉小札》,也就是他俩的书信日记;还有一本其实...
    hsiaoliu晓阅读 3,882评论 0 1