leetcode-Ugly Number

3/8/2017来源:澳门金沙娱乐场人气:24440

Question:

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.

Solution:

class Solution { public: bool isUgly(int num) { if(num == 0){ return false; } while(num != 1){ if(num %2 != 0 && num %3 != 0 && num % 5 != 0){ return false; } if(num%2 == 0){ num /= 2; } if(num%3 == 0){ num /= 3; } if(num%5 == 0){ num /= 5; } } return true; } };

总结:

注意while判断条件是num!=1,而不是 num != 0



网站地图 申博太阳城 太阳城登入 真人百家乐 现金网百家乐
申博代理加盟登入 www.6824.com 太阳城 金太阳国际娱乐网址
老虎机游戏 申博代理 太阳城申博开户 申博官网
太阳城app下载 老虎机游戏 申博棋牌游戏 澳门大三巴赌场
百家乐娱乐登入 盛618网址 申博游戏平台 太阳城集团