【一天一道LeetCode】#263. Ugly Number

来源:互联网 发布:中国和英国的数据差别 编辑:程序博客网 时间:2024/04/28 11:33

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:判断一个数是不是丑数(ugly number),所谓丑数就是只包含因子2,3,5的数。
解题思路:依次除以2,3,5,一直到不能被这三个数整除,如果最后的数为1,则是丑数;如果不为1则不是丑数

class Solution {public:    bool isUgly(int num) {        int priFac[3] = {2,3,5};//三个因子        int cur = num;        while(num>1)        {            int i = 0;            for(; i < 3 ;i++){                if(num%priFac[i]==0) {//能整除就继续                    num/=priFac[i];                    break;                }            }            if(i==3)  return false;//不能整除就返回false        }        return num==1;//最后判断余数等不等于1    }};
0 0