程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> LeetCode -- Ugly Number

LeetCode -- Ugly Number

編輯:C++入門知識

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


判斷一個數是否為ugly number。


對2,3,5執行以下過程:
不斷除n,直到無法整除。


最後剩余數字為1,則該數為ugly number。






實現代碼:

public class Solution {
    public bool IsUgly(int num) 
    {
        if(num <= 0){
            return false;
        }
        num = Divide(num,2);
    	num = Divide(num,3);
    	num = Divide(num,5);
    	
    	return num == 1;
    }


private int Divide(int num, int a)
{
	while(num % a == 0){
		num /= a;
	}
	return num;
}


}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved