NOJ1167 丑陋数 想法题
题意
丑陋数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 丑陋数 想法题的更多相关文章
- HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...
- [LeetCode] 264. Ugly Number II 丑陋数 II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- HDU 4972 Bisharp and Charizard 想法题
Bisharp and Charizard Time Limit: 1 Sec Memory Limit: 256 MB Description Dragon is watching NBA. He ...
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
- [LeetCode] 264. Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] 263. Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
随机推荐
- Xcode_9_beta.xip 更新下载
Xcode_9_beta.xip 更新下载 Xcode_9_beta.xip 链接: pan.baidu.com/s/1dFJ33tJ 密码: 89mv
- 数论day1 —— 基础知识(们)
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61632537 向大(hei)佬(e)势力学(di ...
- [COCI2015]ZMIJA
题目大意: 一个$n\times m(n,m\leq1000)$的格子中有若干金币,从左下角出发,每一步可以进行如下操作: 1.向当前方向前进一格: 2.向上移动一步,并调转当前方向. 一开始的方向是 ...
- UITableView中的dequeueReusableCellWithIdentifier的方法
在使用UITableView控件的时候,datasource的代理方法经常会使用到下面的方法来加载UITableView的数据显示 - (UITableViewCell *)tableView:(UI ...
- How to create an IPA (Xcode 5)
This tutorial will walk you through the easiest way to generate an IPA using Xcode 5. We will be usi ...
- python xml与字典的相互转换
def trans_xml_to_dict(xml): """ 将微信支付交互返回的 XML 格式数据转化为 Python Dict 对象 :param xml: 原始 ...
- Chromatix
1.Lens Rolloff Correction 透镜衰减矫正 The Lens Rolloff correction takes into account the fact that,with ...
- Java.lang.NoClassDefFoundError--找不到相应的类
如题Java.lang.NoClassDefFoundError 错误可能是由于找不到指定的类引起的. 一般出现在java 反射调用,或者通过jniRegisterNativeMethods手动注册j ...
- Shell 进度条效果的一个实现
#!/bin/bash processBar() { now=$ all=$ percent=`awk BEGIN'{printf "%f", ('$now'/'$all')}'` ...
- Oracle基础 存储过程和事务
一.事务和存储过程 在存储过程中如何使用事务.当需要在存储过程中同时执行多条添加.修改.删除SQL语句时,为了保证数据完整性,我们需要使用事务.使用方式和在PL-SQL中非常相似,但也有一些区别. - ...