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 ...
随机推荐
- [HDU6240]Server
题目大意: 用$n$条线段覆盖区间$[1,t]$上的整点.每条线段有4个属性$(S_i,T_i,A_i,B_i)$,表示用第$i$条线段可以覆盖区间$[S_i,T_i]$.若选取线段的集合为$S$,最 ...
- 内存或磁盘空间不足,Microsoft Office Excel 无法再次打开或保存任何文档。 [问题点数:20分,结帖人wenyang2004]
在环境里是没有问题的 就是发布网站后,会出现“/”应用程序中的服务器错误.--------------------------------------------------------------- ...
- SSMS查看表行数以及使用空间 How to show table row count and space used in SSMS - SSMS Tutorials
原文:How to show table row count and space used in SSMS - SSMS Tutorials There's a quick and convenien ...
- Visio中汇总两个箭头
RT,相似度和信任度矩阵融合,两个箭头,选中其中一个,可以选格式--线条--终点选无,或者在快捷那选线条.
- Unity3d UGUI 界面适配 实例解析 三种适配方式
之前做游戏都是用公司自己的引擎,对于界面的适配分Android和IOS两种系统处理. 对于IOS设备,由于数量有限,只要做到 960x640 1024x769 1136x640 (当时还没有ipho ...
- 设计模式之原型模式(php实现)
github地址:https://github.com/ZQCard/design_pattern1.先了解什么是浅拷贝与深拷贝 //深拷贝:赋值时值完全复制,完全的copy,对其中一个作出改变,不会 ...
- Java:网络编程之应用实例
1.聊天 编写一个聊天程序,有收数据的部分和发数据的部分. 这两部分需要同时执行,那么就需要多线程技术. 一个线程控制发送. 一个线程控制接收. 因为收和发动作是不一致的,所以要定义两个run方法,而 ...
- Unicode类别
Unicode 通用类别: http://msdn.microsoft.com/zh-cn/library/20bw873z(VS.80).aspx 类别 说明 Lu 字母,大写 Ll 字母,小写 L ...
- BindVertexbuffer
stride 的意思是 inputstream.layout 的大小 比如 description是 pos uv normal stride 就是一组pos uv normal的大小 ns 里面 ...
- MySQL时间增加、字符串拼接
MySQL时间增加.字符串拼接 SELECT DATE_ADD(startTime, INTERVAL 10 SECOND); CONCAT(string1,string2,…)