poj1338 ugly number 题解 打表
类似的题目有HDU1058 humble number(翻译下来都是丑陋的数字)。
Description
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
shows the first 10 ugly numbers. By convention, 1 is included.
Given the integer n,write a program to find and print the n'th ugly number.
Input
Output
Sample Input
1
2
9
0
Sample Output
1
2
10
题目大意:求出第n个丑数,丑数的定义为该整数的质因数仅有2或3或5或没有质因数(因此1在这道题目也是丑数),当然也可以理解成2,3,5互乘所得到的数字就是丑数,例如15是丑数因为他的质因数仅仅为3,5,28不是丑数,因为他的质因数里面有7
思路:这道题目理解完题意之后最让人头疼的是如何打出一个升序顺序的表,而且这个表要保证这些数要符合题意又不能有所缺漏。我们可以利用定义num2,num3,num5来标记乘过2、3、5的最大数字的下标,当biao[i] == biao[numx] * x的时候(x表示2或3或5),我们让numx ++,而且我们要注意在用if语句判断时不要使用else if 来判断,否则这个表会有几个数字是相同的(例如2*3==6,3*2==6,不过是用else if的话肯定会有两个6)。
代码:
//POJ1338
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define MAXN 1510
using namespace std;
int biao[MAXN + 5];
void makeprime()
{
biao[1] = 1;
int num2 = 1, num3 = 1, num5 = 1;
for(int i = 2;i <= 1510; i ++)
{
biao[i] = min(biao[num2] * 2, min(biao[num3] * 3, biao[num5] * 5));
if(biao[i] == biao[num2] * 2) num2 ++;
if(biao[i] == biao[num3] * 3) num3 ++;
if(biao[i] == biao[num5] * 5) num5 ++;
}
}
int main()
{
makeprime();
int n;
while(scanf("%d", &n) != EOF, n)
{
printf("%d\n", biao[n]);
}
return 0;
}
poj1338 ugly number 题解 打表的更多相关文章
- [POJ1338]Ugly Numbers
[POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- 313. Super Ugly Number
题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...
- <LeetCode OJ> 26 / 264 / 313 Ugly Number (I / II / III)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- [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 ...
- 【13_263】Ugly Number
简单题 Ugly Number My Submissions Question Total Accepted: 32635 Total Submissions: 94009 Difficulty: E ...
- Leetcode 313. super ugly number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 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
Problem: Write a program to check whether a given number is an ugly number. Ugly numbers are positiv ...
随机推荐
- dotnet 理解 IConfigurationProvider 的 GetChildKeys 方法用途
我最近遇到了一个有趣的 Bug 让我调试了半天,这个 Bug 的现象是我的好多个模块都因为读取不到配置信息而炸掉,开始我没有定位到具体的问题,以为是我的配置服务器挂掉了.经过了半天的调试,才找到了是我 ...
- C++里也有菱形运算符?
最近在翻<c++函数式编程>的时候看到有一小节在说c++14新增了"菱形运算符".我寻思c++里好像没什么运算符叫这名字啊,而且c++14新增的功能很少,我也不记得有添 ...
- Python第三方库的安装和导入
目录 一.Python第三方库的安装 1. 使用pip命令行安装 2. 使用PyCharm进行安装 3. 下载第三方库文件到本地进行安装 4. 通过国内源进行安装 二.Python第三方库的导入 1. ...
- 2020年9月至10月 Splashtop 新功能
Splashtop 已为 Splashtop Business Access.Splashtop Remote Support.Splashtop SOS 和 Splashtop On-Prem ...
- C++ placement new学习
通常创建对象使用new操作,但这样无法指定在具体某一块内存开辟空间创建对象.而如果 可以指定开辟空间的内存位置,我们可以编写内存池高效的复用同一个内存位置,这样可以避免系统频繁申请可用内存 所占用的时 ...
- MindSpore梯度进阶操作
技术背景 在MindSpore深度学习框架中,我们可以使用mindspore.grad对函数式编程的函数直接计算自动微分,也可以使用mindspore.ops.GradOperation求解Cell类 ...
- kubernetes 之dashboard
部署 kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.0/aio/deploy/recomme ...
- Javascript/DOM:如何删除 DOM 对象的所有事件侦听器
Javascript/DOM:如何删除 DOM 对象的所有事件侦听器 一.重写 重写 EventTarget 添加监听事件方法 addEventListener if (EventTarget.pro ...
- NumPy 数组排序、过滤与随机数生成详解
NumPy 数组排序 排序数组 排序数组意味着将元素按特定顺序排列.顺序可以是数字大小.字母顺序.升序或降序等. NumPy 的 ndarray 对象提供了一个名为 sort() 的函数,用于对数组进 ...
- iis worker process w3wp 进程 占用率100%
今天电脑特别的卡,我没当回事,但是实在是卡得不行了,我打开任务管理器,发现 iis worker process 进程已经快100%了,我之前在iis上发布了一个webservice,我就把这个网站给 ...