LEETCODE-Ugly Number

来源:互联网 发布:c语言阶乘函数 编辑:程序博客网 时间:2024/06/03 14:59

leetcode
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.

#include<stdio.h>#include<iostream>#include<cstring>#define MAXN 300+10using namespace std;bool isUgly(int num) {        int i = 1;        if( num <= 0)            i = 0;        if( num == -2147483648 )            i = 0;        while( num >= 2)            {            if((num%2)==0){                num = num / 2;                continue;            }            if((num%3)==0){                num = num / 3;                continue;            }            if((num%5)==0){                num = num / 5;                continue;            }            else{                i = 0;                break;            }        }        if(i == 1)            return 1;        else            return 0;    }int main(){    int num;    bool t;    cin >> num;    t = isUgly(num);    cout << t;}https://leetcode.com/submissions/detail/38376104/
0 0