【13_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.
Discuss中总有惊喜:
Java:
for (int i=2; i<6 && num>0; i++)
while (num % i == 0)
num /= i;
return num == 1;
Python:
for p in 2, 3, 5:
while num % p == 0 < num:
num /= p
return num == 1
C++:(其中&&之后的num怎么理解?)
for (int i=; i< && num; i++)
while (num % i == )
num /= i;
return num == ;
自己的:
C++:
class Solution {
public:
bool isUgly(int num) {
if (num <= )
return false;
if (num == )
return true;
while (num != ) {
if (num % == ) {
num = num / ;
}
else if (num % == ) {
num = num / ;
}
else if (num % == ) {
num = num / ;
}
else{
return false;
}
}
return true;
}
};
【13_263】Ugly Number的更多相关文章
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【HDU3948】 The Number of Palindromes (后缀数组+RMQ)
The Number of Palindromes Problem Description Now, you are given a string S. We want to know how man ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
- 【u020】Couple number
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple numb ...
随机推荐
- C# 根据类名称创建类示例
//获得类所在的程序集名称(此处我选择当前程序集) string bllName = System.IO.Path.GetFileNameWithoutExtension(System.Reflect ...
- iOS手机功能汇总
开发中经常会调用手机功能,今天来汇总一下,若有不足欢迎大家指出,下面分别介绍如下功能 : 电话 短信 邮件 通讯录 定位 跳转应用 跳转App Store 打开其他文件 电话 调用电话有下图两种不同样 ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- 【转】MySQL连接超时断开的问题
这遍文章转过来做个笔记,时不时看看. 转:http://blog.csdn.net/nethibernate/article/details/6658855 Exception如下: org.hi ...
- 产生某个区间的随机整数 int #Java
int max = Integer.MAX_VALUE; int min = 1; Random random = new Random(); int s = random.nextInt(max)% ...
- css3 animation动画特效插件的巧用
这一个是css3 animation动画特效在线演示的网站 https://daneden.github.io/animate.css/ 下载 animate.css文件,文件的代码很多,不过要明白 ...
- css之absolute绝对定位(技巧篇)
无依赖的绝对定位 margin,text-align与绝对定位的巧妙用法 例子1:实现左右上角的图标覆盖,如图,
- 过滤Xss
/** * 防xss过滤 * * @author rentingshuang <tingshuang@rrkd.cn> * @param type $string * @param typ ...
- SqlServer2008 无法修改表,超时时间已到 在操作完成之前超时解决方法
在 SQL Server Management Studio 里, 通过菜单“工具-选项”打开选项对话框. 在左侧寻找“设计器-表设计器和数据库设计器”, 然后在右侧勾选“为表设计器更新重写连接字符串 ...
- javascript eval和JSON之间的联系
原出处:http://www.jb51.net/article/21688.htm eval函数的工作原理 eval函数会评估一个给定的含有JavaScript代码的字符串,并且试图去执行包含在字符串 ...