题意

丑陋数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. Struts2笔记--文件下载

    Struts2提供了stream结果类型,该结果类型是专门用于支持文件下载功能的.配置stream类型的结果需要指定以下4个属性. contentType:指定被下载文件的文件类型 inputName ...

  2. Java判断中文字符

    package com.jsoft.test; import java.util.regex.Pattern; /** * 判断中文字符 * * @author jim * @date 2017-12 ...

  3. 事件溯源模式(Event Sourcing Pattern)

    此文翻译自msdn,侵删. 原文地址:https://msdn.microsoft.com/en-us/library/dn589792.aspx 本文介绍了一种有利于物化(materialize)领 ...

  4. 监控SQL Server正在执行的SQL语句和死锁情况

    原文:监控SQL Server正在执行的SQL语句和死锁情况 SELECT [Individual Query] = SUBSTRING(qt.TEXT, er.statement_start_off ...

  5. 50个最常用的UNIX/Linux命令

    转自http://get.jobdeer.com/493.get 1. tar command examples Create a new tar archive. $ tar cvf archive ...

  6. JavaScript的filter用法

    Js的有些操作会改变原来的对象:有些操作则不会改变原来对象. 数组的filter方法就不会改变原来数组 利用filter,可以巧妙地去除Array的重复元素: 'use strict'; var r, ...

  7. hmac的python实现

    Hash-based message authentication code,利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出 可以查看python的内置模块hmac.py的源码来学 ...

  8. 11款最棒的Linux数据恢复工具

    无论你使用的是台式电脑还是笔记本,需要关注的重点之一都是如何保护好你的宝贵数据.因为总会有各种突发情况使你的系统崩溃,然后你要做的就是恢复数据.不管你怎么想,要是我失去了所有的数据却无法恢复的话,我会 ...

  9. python 常用的模块(base64)转

    Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打开exe.jpg.pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多无法显示和打印的字符,所以,如果要让记事本这样的 ...

  10. python解析json文件报错No JSON object could be decoded

    2017-04-25 可用Nodepad++将json文件打开并以UTF8无BOM格式保存.