NOJ1167 丑陋数 想法题
题意
丑陋数n的意思是n的全部素数因子仅仅有2,3,5。
求出前1500个丑陋数。
(第一个丑陋数是1)
思路
用一个数组维护全部的丑陋数。
一開始数组中仅仅有一个数就是1。
如今能够确定的丑陋数还有1*2,1*3,1*5。把这三个数中最小的2放进数组。
然后变成了2*2,1*3,1*5。
再把最小的一个数放进数组…依次运行下去,直到倒数第三个数填满整个丑陋数数组。
用c2 c3 c5确定眼下的和2 3 5相乘的丑陋数。
注意推断三个数有相等的情况。
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1510;
int s[maxn];
int main()
{
s[1] = 1;
int c2=1,c3=1,c5=1;
for(int i = 2 ; i < maxn ; i ++) {
//?
??
?
s[i]???
?
int s2 = s[c2]*2,s3 = s[c3]*3,s5 = s[c5]*5;
if(s2 < s3) {
if(s2 < s5) {
s[i] = s2;
c2 ++;
continue;
}else if(s2 > s5) {
s[i] = s5;
c5 ++;
continue;
}else {
s[i] = s5;
c2 ++; c5 ++;
continue;
}
}else if(s2 > s3) {
if(s3 < s5) {
s[i] = s3;
c3 ++;
continue;
}else if(s3 > s5) {
s[i] = s5;
c5 ++;
continue;
}else {
s[i] = s5;
c5 ++; c3 ++;
continue;
}
}else {//s2 = s3
if(s2 < s5) {
s[i] = s2;
c2 ++; c3 ++;
continue;
}else if(s2 > s5){
s[i] = s5;
c5 ++;
continue;
}else {
s[i] = s2;
c2 ++; c3 ++; c5 ++;
continue;
}
}
}
int n;
while(scanf("%d",&n) && n) printf("%d\n",s[n]);
return 0;
}
NOJ1167 丑陋数 想法题的更多相关文章
- HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...
- [LeetCode] 264. Ugly Number II 丑陋数 II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- HDU 4972 Bisharp and Charizard 想法题
Bisharp and Charizard Time Limit: 1 Sec Memory Limit: 256 MB Description Dragon is watching NBA. He ...
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
- [LeetCode] 264. Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] 263. Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
随机推荐
- 【noip2017 day2T2】【蚯蚓】巧用队列单调性线性处理
(画师当然是武内崇啦) Description 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」=[3.9」=3.蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐 ...
- 工作组模式下专用队列(Private Queue)如何引用远程队列路径
查了N久资料,包括MSDN的官方文档,对于同一工作组下,不同机器之间如何利用Private Queue(专用队列)来发送/接收消息,关于Path的引用一说,无非都是MachineName\privat ...
- lrc 校验码 ascii 格式
lrc 校验码 ascii 格式 将adr1 (站号)至最后一个数据内容相加,得到结果以256为单位,超出部分去除(如得到的结果为16#128H则只取28H,) 然后计算二次反补得到后的结果即为侦误 ...
- superobject使用方法
superobject使用方法 ISuperObject.AsObject 可获取一个 TSuperTableString 对象. TSuperTableString 的常用属性: count.Get ...
- 【java】Stream的使用
首先,给大家推荐一个好的地方:http://ifeve.com/stream/ 可以好好学一下 接下来,今天要删除数组中的某些元素,想到了之前用过的这个JDK8的Stream 1.Array转化为St ...
- UVA 1665 Islands
题意:输入一个n*m矩阵,每一个格子都有一个正整数,再输入T个整数ti,对于每一个ti,输出大于ti的正整数组成多少个四连快 思路:正着做的话事实上相当于删除连通块,而假设反着做的话就相当于变成添加连 ...
- 【Hadoop】Hadoop MR 性能优化 Combiner机制
1.概念 2.参考资料 提高hadoop的mapreduce job效率笔记之二(尽量的用Combiner) :http://sishuo(k).com/forum/blogPost/list/582 ...
- Java 学习之网络编程案例
网络编程案例 一,概念 1,网络编程不等于网站编程 2,编程只和传输层打交道,即TCP和UDP两个协议 二,案例 1,TCP实现点对点的聊天 Server端:两个输入流:读客户端和控制台,一个输出端: ...
- json-server模拟接口获取mock数据
转载:http://blog.csdn.net/stevennest/article/details/76167343 安装json-server 运行以下命令 cnpm install json-s ...
- VirtualBox修改现有虚拟磁盘大小
VirtualBox装Ubuntu下载Android源代码分配的磁盘空间不够,修改磁盘大小必须进入VirtualBox安装目录使用VBoxmanager修改,执行: vboxmange modifyh ...