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 ...
随机推荐
- [FAQ] Beego2.0.2 bee 生成的 api 项目运行 404, http server Running on http://:8080
Beego, bee version 2.0.2 https://github.com/beego/beego/issues/4363 Tool:AI 编程助手 Refer:Beego还流行吗 Lin ...
- Quill富文本编辑器的实践 - DevUI
DevUI 是一款面向企业中后台产品的开源前端解决方案,它倡导沉浸.灵活.至简的设计价值观,提倡设计者为真实的需求服务,为多数人的设计,拒绝哗众取宠.取悦眼球的设计.如果你正在开发 ToB 的工具类产 ...
- UNO 的 SamplesApp.Skia.Gtk 丢失字体文件抛出空异常
在运行 UNO 的 SamplesApp.Skia.Gtk 例子程序时,如果没有拷贝字体文件夹,导致字体丢失,将会在运行的时候抛出 NullReferenceException 空异常 抛出的异常堆栈 ...
- 为 RabbitMQ 服务器启用 SSL/TLS
为 RabbitMQ 服务器启用 SSL/TLS 目录 为 RabbitMQ 服务器启用 SSL/TLS 为客户端和服务器生成自签名证书 在 RabbitMQ 服务器中启用 TLS/SSL 支持 使用 ...
- 网站访问速度优化实战:CDN源/Nginx压缩/全站CDN加速
前言 接触到CDN的起因: 我自己搭建的网站https://price.monitor4all.cn/网页打开的速度一直比较慢,经查证是我的网站有很多静态js大文件,通过浏览器读取这些js比较耗时间. ...
- HZ2023 远足游记
你说得对,但是我放假之前写的 P4689 代码没了 所以来摆 4.6(远足) 上午 走路,刚开始感觉没啥 走到园博园发现没预料中那么顺利 但是还感觉没啥 因为也没预料到 \(N·m\) 学校会让我们原 ...
- 介绍几种常用的Oracle客户端工具
首发微信公众号:SQL数据库运维 原文链接:https://mp.weixin.qq.com/s?__biz=MzI1NTQyNzg3MQ==&mid=2247485212&idx=1 ...
- vue和react的相同点和不同点
Vue和React作为现代前端开发中流行的两个JavaScript框架,它们有诸多相似之处,同时也存在一些关键性的不同.以下是Vue和React的一些主要相同点和不同点: 相同点: 虚拟DOM:Vue ...
- fuser命令详解
fuser -mv 作用 fuser命令是用来显示所有正在使用着指定的file.file system或者sockets的进程信息.具体来说,fuser -mv的作用如下: 参数-m:指定一个被加载的 ...
- dbeaver使用详解
1.dbeaver使用本地驱动 解压 点击可执行文件 驱动管理 新建驱动 起名字 com.mysql.jdbc.Driver jdbc:mysql//{host}[:{port}]/[{databas ...