问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3862 访问。

编写一个程序判断给定的数是否为丑数。丑数就是只包含质因数 2, 3, 5 的正整数。

输入: 6

输出: true

解释: 6 = 2 × 3

输入: 8

输出: true

解释: 8 = 2 × 2 × 2

输入: 14

输出: false

解释: 14 不是丑数,因为它包含了另外一个质因数 7。

说明:

  1. 1 是丑数。
  2. 输入不会超过 32 位有符号整数的范围: [−231,  231 − 1]。

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.

Input: 6

Output: true

Explanation: 6 = 2 × 3

Input: 8

Output: true

Explanation: 8 = 2 × 2 × 2

Input: 14

Output: false

Explanation: 14 is not ugly since it includes another prime factor 7.

Note:

  1. 1 is typically treated as an ugly number.
  2. Input is within the 32-bit signed integer range: [−231,  231 − 1].

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3862 访问。

public class Program {

    public static void Main(string[] args) {
var n = 60;
var res = IsUgly(n);
Console.WriteLine(res); Console.ReadKey();
} private static bool IsUgly(int num) {
var uglyList = new int[] { 2, 3, 5 };
foreach(var ugly in uglyList) {
while(num % ugly == 0 && (num /= ugly) > 0) { }
}
return num == 1;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3862 访问。

True

分析:

显而易见,以上算法的时间复杂度为:  。

最新文章

  1. ceil 模块
  2. myeclipse 第一步
  3. PAT1023. Have Fun with Numbers
  4. poj 1364
  5. easyui的combobox将得到的数据设定为下拉框默认值和复选框设定默认值
  6. AspectJ给类的属性打桩,进行替换。
  7. c# ProxyServer 代理服务器 不是很稳定
  8. 【Oracle】控制文件管理
  9. make,makefile,cmake“暴力编译法”的个人经验或理解。
  10. C语言典型编程3
  11. MT【255】伸缩变换
  12. 表单相关标签之textarea,select
  13. yii2 支付宝支付教程 [ 2.0 版本 ]
  14. Wifi 开放系统认证和共享密钥身份认证
  15. Atitit js canvas的图像处理类库attilax总结与事业
  16. bulid-tool
  17. egret -纹理集的制作
  18. Xiuno BBS 3.0 轻论坛程序正式版发布。
  19. 正则表达式入门(c#)
  20. Zend_Controller_Front 研究

热门文章

  1. for语句例题:编写程序FooBizBaz.java,从1循环到150并在每行打印一个值
  2. 搭建sonarqube分析golang代码
  3. css初始化表(normalize.css)
  4. STL源码剖析:关联式容器
  5. Spark 3.0 新特性 之 自适应查询与分区动态裁剪
  6. Get与Post的区别?(面试官最想听到的答案)
  7. turtle库常用函数
  8. pandas第三方库
  9. ASP.NET Core 监听SQL Server数据库的实时信息
  10. Python unichr() 函数