码迷,mamicode.com
首页 > 编程语言 > 详细

[leetcode]Ugly Number 解题报告 C语言

时间:2015-08-30 17:41:29      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:c语言   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.

【具体代码如下】

bool isUgly(int num) 
{
    if(num<=0)return false;
    if(num==1)return true;
    int check=num;
    bool flag=true;
    while(flag==true)
    {
    int cs;

    if(check%2==0)
           cs=1;
    else if(check%3==0)
           cs=2;
    else if(check%5==0)
           cs=3;
    else cs=0;
    switch(cs){
        case 1:check=check/2;break;
        case 2:check=check/3;break;
        case 3:check=check/5;break;
        case 0:flag=false;return false;
         }
    if(check==1)return true;
    }
    return flag;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

[leetcode]Ugly Number 解题报告 C语言

标签:c语言   leetcode   

原文地址:http://blog.csdn.net/noc_lemontree/article/details/48105679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!