题意

丑陋数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. phpredis Floating point exception

    发生在高版本的gcc编译后的so用于低版本gcc编译出来的php, 解决方法, 加上参数-Wl,--hash-style=sysv phpize ./configure vi Makefile CC ...

  2. JsonArray对象

    直接上代码: private static JSONObject createJSONObject() { JSONObject jsonObject = new JSONObject(); json ...

  3. jq和js中click 事件的几种方式总结和click事件的累加问题解决办法

     1:常见的三种绑定click事件: 第一种:$("#click").click(function(){ alert("Hello World  click") ...

  4. 设计模式之工厂模式之简单工厂(php实现)

    github源码地址: git@github.com:ZQCard/design_pattern.git 1.简单工厂模式 特点:将调用者与创建者分离,调用者直接向工厂请求,减少代码的耦合.提高系统的 ...

  5. quartz---任务调度小试(多任务)

    quartz---任务调度小试         背景 笔者眼下做的项目"jrkj"首页上的信息都是从redis中读取的,每小时更新一次存入redis中,那么问题来了怎么才干让系统每 ...

  6. Qt中的非模式窗口配置;

    Test7_5A::Test7_5A(QWidget *parent) : QMainWindow(parent){ ui.setupUi(this); m_searchwin = new Searc ...

  7. ISP模块之RAW DATA去噪(一)

    ISP(Image Signal Processor),图像信号处理器,主要用来对前端图像传感器输出信号处理的单元,主要用于手机,监控摄像头等设备上. RAW DATA,可以理解为:RAW图像就是CM ...

  8. Tomcat7.0源代码分析——启动与停止服务原理

    前言 熟悉Tomcat的project师们.肯定都知道Tomcat是怎样启动与停止的. 对于startup.sh.startup.bat.shutdown.sh.shutdown.bat等脚本或者批处 ...

  9. android 中怎样获取IMEI号

    1)在Telephony Framework内部.能够直接使用GSMPhone或GeminiPhone提供的接口. KK之前的版本号: IMEI(International Mobile Equipm ...

  10. Java程序猿的JavaScript学习笔记(12——jQuery-扩展选择器)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...