题意

丑陋数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 丑陋数 想法题的更多相关文章

  1. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  2. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  3. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  4. [LeetCode] Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  5. [LeetCode] Ugly Number 丑陋数

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

  6. HDU 4972 Bisharp and Charizard 想法题

    Bisharp and Charizard Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching NBA. He ...

  7. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

  8. [LeetCode] 264. Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

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

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

随机推荐

  1. 集群/分布式/微服务/SOA 转

    https://www.cnblogs.com/Java3y/p/9479410.html 二.集群/分布式/微服务/SOA是什么? 像我这种技术小白,看到这些词(集群/分布式/微服务/SOA)的时候 ...

  2. My first blog on cnBlogs!

    以后会长期更新自己的心得体会!以此锻炼自己,奋发向前.

  3. KVM Network Bridging

    from http://hzqtc.github.io/2012/02/kvm-network-bridging.html http://wiki.ubuntu.org.cn/Kvm_%E7%BD%9 ...

  4. c++中resize这个函数怎么用

    c++中序列式容器的一个共性函数, vv.resize(int n,element)表示调整容器vv的大小为n,扩容后的每个元素的值为element,默认为0 resize()会改变容器的容量和当前元 ...

  5. ostu进行遥感图像的分割

    城市地区道路网的简单的阈值分割.采用的是单ostu(最佳阈值分割)算法,废话少说,如果不太清楚该算法,请参考文献[1]中的图像分割这一章的介绍.程序直接运行的效果如下.

  6. 学习一些和redux一样作用的mobx知识

    两个组件:mobx和mobx-react 英文文档:https://mobx.js.org/refguide/object.html 中文文档:https://cn.mobx.js.org/ 样例:h ...

  7. hive启动报错:system:java.io.tmpdir等

    解决方法:在hive-site.xml中添加 <property>    <name>system:java.io.tmpdir</name>    <val ...

  8. 2017.9.5 postgresql加密函数的使用

    需要安装的插件的名字:pgcrypto 官网地址:https://www.postgresql.org/docs/9.4/static/pgcrypto.html stackoverflow: htt ...

  9. Python按行读取文件、写文件

    Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...

  10. MySQL的左外连接

    代码: select t1.descid, IFNULL(t2.cnt,) as countnew, t1.description from uosdesc t1 left outer join t2 ...