263. Ugly Number(C++)

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

所谓丑数,就是不能被2,3,5以外的其他素数整除的数。1,2,3,4,5,6,8,9,10,12,15是最前面的11个丑数。

题目大意:

编写程序判断一个给定的数字是否为“丑陋数” ugly number

丑陋数是指只包含质因子2, 3, 5的正整数。例如,6, 8是丑陋数而14不是,因为它包含额外的质因子7

注意,数字1也被视为丑陋数

结题方法:

让其重复的除2,3,5

C++代码:

 1 class Solution {
 2 public:
 3     bool isUgly(int num) {
 4         if(num<=0) return false;
 5         vector<int> v{2,3,5};
 6
 7         for(auto c:v)
 8         {
 9             while(num%c==0) num/=c;
10         }
11         return num==1;
12     }
13 };
时间: 2024-10-11 01:26:57

263. Ugly Number(C++)的相关文章

Leetcode 263. Ugly Number

263. Ugly Number Total Accepted: 65061 Total Submissions:173755 Difficulty: Easy 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

263. Ugly Number &amp;&amp; 264. Ugly Number II &amp;&amp; 313. Super Ugly Number

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

Leetcode 263. Ugly Number JAVA语言

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 ty

【LeetCode】263. Ugly Number

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

leetcode:263 Ugly Number-每日编程第十七题

Ugly Number Total Accepted: 31482 Total Submissions: 90982 Difficulty: Easy 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 whil

Java [Leetcode 263]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

(easy)LeetCode 263.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 ty

263. Ugly Number

一定要注意…………审题!!!! 1 public boolean isUgly(int num) { 2 if(num <= 0) { 3 return false; 4 } 5 if(num == 1) { 6 return true; 7 } 8 while(num % 2 == 0) { 9 num /= 2; 10 } 11 while(num % 3 == 0) { 12 num /= 3; 13 } 14 while(num % 5 == 0) { 15 num /= 5; 16 }

LeetCode之263. Ugly Number

------------------------------------------------------------- 如果一个数的质因子只包括2,3,5,那么这个数n可以表示为:n=2x+3y+5z AC代码: import java.math.BigInteger; public class Solution { public boolean isUgly(int n) { if(n<1) return false; while(n%2==0) n/=2; while(n%3==0) n