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.

  1. /* 2ms */
  2.  
  3. public class Solution {
  4.  
  5. int quyu(int a,int q){
  6. while(a%q==0){
  7. a=a/q;
  8. }return a;
  9. }
  10. public boolean isUgly(int num) {
  11.  
  12. if(num==0){
  13. return false;
  14. }
  15. num=quyu(num,2);
  16. num=quyu(num,3);
  17. num=quyu(num,5);
  18.  
  19. if(num==1){
  20. return true;
  21. }else{
  22. return false;
  23. }
  24. }
  25. }
  1. /* 5ms */
  2.  
  3. public class Solution {
  4.  
  5. public boolean isUgly(int num) {
  6.  
  7. if(num==0) return false;
  8. while(num%2==0) num/=2;
  9. while(num%3==0) num/=3;
  10. while(num%5==0) num/=5;
  11.  
  12. if(num==1) return true;
  13. else return false;
  14. }
  15. }
  1. /*************************************************************************
  2. > File Name: LeetCode263.c
  3. > Author: Juntaran
  4. > Mail: JuntaranMail@gmail.com
  5. > Created Time: Wed 18 May 2016 19:45:08 PM CST
  6. ************************************************************************/
  7.  
  8. /*************************************************************************
  9.  
  10. Ugly Number
  11.  
  12. Write a program to check whether a given number is an ugly number.
  13.  
  14. Ugly numbers are positive numbers whose prime factors
  15. only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly
  16. since it includes another prime factor 7.
  17.  
  18. Note that 1 is typically treated as an ugly number.
  19.  
  20. ************************************************************************/
  21.  
  22. #include <stdio.h>
  23.  
  24. int quyu( int num, int q )
  25. {
  26. while( num % q == )
  27. {
  28. num = num / q;
  29. }
  30. return num;
  31. }
  32.  
  33. int isUgly(int num)
  34. {
  35. if( num <= )
  36. {
  37. return ;
  38. }
  39. num = quyu( num, );
  40. num = quyu( num, );
  41. num = quyu( num, );
  42.  
  43. if( num == )
  44. {
  45. return ;
  46. }
  47. else
  48. {
  49. return ;
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. int num = ;
  56. int ret = isUgly(num);
  57. printf("%d\n", ret);
  58. }

LeetCode 263的更多相关文章

  1. leetcode@ [263/264] Ugly Numbers & Ugly Number II

    https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...

  2. [LeetCode] 263. Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  3. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  4. LN : leetcode 263 Ugly Number

    lc 263 Ugly Number lc 263 Ugly Number Write a program to check whether a given number is an ugly num ...

  5. Java实现 LeetCode 263 丑数

    263. 丑数 编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 输 ...

  6. LeetCode 263 Ugly Number

    Problem: Write a program to check whether a given number is an ugly number. Ugly numbers are positiv ...

  7. Leetcode 263 Ugly Number 数论 类似质因数分解

    Ugly Number的质因数仅为2,3,5 将输入的数分别除以2,3,5直到不能除,看是否为1,为1的是Ugly Number,其他则不是. class Solution { public: boo ...

  8. (easy)LeetCode 263.Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  9. Java [Leetcode 263]Ugly Number

    题目描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...

随机推荐

  1. HDU 5833 Zhu and 772002 (高斯消元)

    Zhu and 772002 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5833 Description Zhu and 772002 are b ...

  2. PID参数整定快速入门(调节器参数整定方法)

    PID调节器参数整定方法很多,常见的工程整定方法有临界比例度法.衰减曲线法和经验法.云润仪表以图文形式分别介绍调节器参数整定方法. 临界比例度法一个调节系统,在阶跃干扰作用下,出现既不发散也不衰减的等 ...

  3. ALAsset,ALAssetsLibrary,ALAssetsgroup常见属性及用法

    转载自  http://www.cnblogs.com/javawebsoa/archive/2013/07/19/3201246.html ALAssetsgroup --------------- ...

  4. oracle 的rowid和rownum

    rowid就是唯一标志记录物理位置的一个id,对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行 ...

  5. POJ 3041 Asteroids (二分图最小点覆盖)

    题目链接:http://poj.org/problem?id=3041 在一个n*n的地图中,有m和障碍物,你每一次可以消除一行或者一列的障碍物,问你最少消除几次可以将障碍物全部清除. 用二分图将行( ...

  6. UVa 10817 Headmaster's Headache (状压DP+记忆化搜索)

    题意:一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师.每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两个老 ...

  7. HTML5学习笔记(一):HTML简介

    Web前端涵盖的内容较多且杂,主要由3个部分组成:HTML标记语言.CSS样式语言和JavaScript脚本语言组成,而下面我们将先学习最新的标记语言HTML5. <!DOCTYPE>标记 ...

  8. C++中标准容器Vector,元素操作.insert()小结

    insert() 函数有以下三种用法: iterator insert( iterator loc, const TYPE &val );  //在指定位置loc前插入值为val的元素,返回指 ...

  9. Redis学习_01 windows下的环境搭建

    一.Redis 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset( ...

  10. Mysql,JDBC封装

    1.完成对数据库的表的增删改的操作 2.查询返回单条记录 3.查询返回多行记录 4.可以使用反射机制来封装,查询单条记录 5.反射机制,查询多条记录 package myjdbc; import ja ...