题意

丑陋数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. Codeforces Round #325 (Div. 2) Alena's Schedule 模拟

    原题链接:http://codeforces.com/contest/586/problem/A 题意: 大概就是给你个序列..瞎比让你统计统计什么长度 题解: 就瞎比搞搞就好 代码: #includ ...

  2. manacher(马拉车)算法详解+例题一道【bzoj3790】【神奇项链】

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=39091399 (CSDN好像有bug,不知道为什 ...

  3. 收纳箱1号 | GitHub Pages部署静态网页的一点私货

    Static site 总结各种各有的 static site generator Jekyll 其实是一个 static site generator. 如果你去 Google 这个,会发现有很多总 ...

  4. Linux下安装PHP的GD支持库

    Linux下安装PHP的GD支持库   1.安装 zlib  wget ftp://ftp.sunfreeware.com/pub/freeware/SOURCES/zlib-1.2.3.tar.gz ...

  5. Android Developer -- Bluetooth篇 开发实例之四 API详解

    http://www.open-open.com/lib/view/open1390879771695.html 这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每 ...

  6. 写给嵌入式程序员的循环冗余校验(CRC)算法入门引导

    写给嵌入式程序员的循环冗余校验(CRC)算法入门引导 http://blog.csdn.net/liyuanbhu/article/details/7882789 前言 CRC校验(循环冗余校验)是数 ...

  7. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  8. centos7 安装LNMP(php7)之php7.0 yum安装

    http://www.jianshu.com/p/35f21210668a 安装过程参考上面的网址

  9. Makefile之嵌套执行make

    1.在Makefile中 一般使用一个总控Makefile来指明文件的编译规则 如: subsystem: cd subdir && $(MAKE) 其等价于 subsystem: $ ...

  10. BeagleBone Black板第四课:简单LED控制实验

    BBB板第四课:简单LED控制实验 学习BBB板的终于目的是想像单片机一样做控制,但控制思路全然不一样(Linux下控制硬件设备实质就是对相关设备虚拟文件的读写).研究了几天头都大了还是没有进展,网上 ...