题意

丑陋数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. 「JXOI2018」游戏

    注意输出的应该是 所有方案的和,,而不是期望. 我们不妨把依赖关系建图,可以发现 所有没有入度的点都被查水表了一次 是 游戏结束的 充要条件. 于是我们只需要知道有多少没有入度的点,然后再排列算一算就 ...

  2. Mybatis中的XML中需要用到的转义符号整理

    使用这么久的Mybatis中需要转义的符号整理一下,小结一下: 1.       <         小于符号        < 2.       <=       小于等于     ...

  3. Intel VTune性能分析器基础

    https://wenku.baidu.com/view/b0fe162ebd64783e09122b66.html

  4. XCTest(二)

    New tool sets are making it easier and easier to engage in genuine agile development on iOS. In part ...

  5. [置顶] kubernetes资源类型--pod和job

    pod Pod是K8S的最小操作单元,一个Pod可以由一个或多个容器组成:整个K8S系统都是围绕着Pod展开的,比如如何部署运行Pod.如何保证Pod的数量.如何访问Pod等. 特点 Pod是能够被创 ...

  6. jquery调用click事件的三种方式

    第一种方式: $(document).ready(function(){ $("#clickme").click(function(){ alert("Hello Wor ...

  7. java中XML操作:xml与string互转、读取XML文档节点及对XML节点增删改查

    一.XML和String互转: 使用dom4j程式变得很简单 //字符串转XML String xmlStr = \"......\"; Document document = D ...

  8. DatagramPacket,DatagramSocket

    package test; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSo ...

  9. 关于批量插入数据之我见(100万级别的数据,mysql)

    因前段时间去面试,问到怎样高效向数据库插入10万条记录,之前没处理过类似问题.也没看过相关资料,结果没答上来,今天就查了些资料.总结出三种方法: 測试数据库为mysql!!! 方法一: public ...

  10. Sublime Text 3 文档

    中文版:http://feliving.github.io/Sublime-Text-3-Documentation/ 英文版:http://www.sublimetext.com/docs/3/