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

【题目】

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;

}

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

时间: 2024-11-05 18:44:33

[leetcode]Ugly Number 解题报告 C语言的相关文章

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

LeetCode: Largest Number 解题报告

Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string

[leetcode]Count Primes 解题报告 C语言

[题目] Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. [题目分析] 这道题常用的判断一个数是否为质数是行不通的,根据hint,采用Sieve of Eratosthenes算法实现,具体关于该算法详见https://en.w

[leetcode]Valid Anagram解题报告 C语言

[题目] Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string cont

[leetcode]Isomorphic Strings 解题报告 C语言

[题目] Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of charact

[leetcode]Valid Sudoku 解题报告 C 语言

[题目] Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. Note: A valid Sudoku board (partially filled) is not necessarily solvable.

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,