类似的题目有HDU1058 humble number(翻译下来都是丑陋的数字)。

Description

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 

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

Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.

Output

For each line, output the n’th ugly number .:Don’t deal with the line with n=0.

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 题解 打表的更多相关文章

  1. [POJ1338]Ugly Numbers

    [POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...

  2. 313. Super Ugly Number

    题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...

  3. &lt;LeetCode OJ&gt; 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 ...

  4. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  5. [LeetCode] Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  6. [LeetCode] Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  7. 【13_263】Ugly Number

    简单题 Ugly Number My Submissions Question Total Accepted: 32635 Total Submissions: 94009 Difficulty: E ...

  8. Leetcode 313. super ugly number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  9. Leetcode 264. Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  10. LeetCode 263 Ugly Number

    Problem: Write a program to check whether a given number is an ugly number. Ugly numbers are positiv ...

随机推荐

  1. [PHP] 几个拖慢 PHP 程序/API 运行速度的点

    1. 启动.查找 Session 需要一定开销,默认 session.save_handler=files,可以通过修改为 redis 提速. files 的 session 会阻塞请求?https: ...

  2. WPF 对接 Vortice 绘制 WIC 图片

    本文告诉大家如何通过 Vortice 在 Direct2D 里面绘制图片,图片的来源是 WIC 加载出的图片 在上一篇博客 WPF 对接 Vortice 调用 WIC 加载图片 告诉了大家如何对接 V ...

  3. 使用WebSocket实现实时多人答题对战游戏

    前言 前两章教程,我们使用WebSocket的基础特性打造了一个小小聊天室,并在第二章对其进行了集群化改造. 系列教程回顾: [WebSocket]第一章:手把手搭建WebSocket多人在线聊天室( ...

  4. Dinky实时计算平台

    前言:Apache Flink 作为新一代的实时计算框架已经被应用到各个行业与领域,其岂存在着应用的痛点比如 FlinkSQL 在线IDE.作业提交不友好.作业无监控报警等.很大程度上说,FlinkS ...

  5. 基于改进MFCC特征和卷积递归神经网络的心音分类

    具体的软硬件实现点击http://mcu-ai.com/MCU-AI技术网页_MCU-AI人工智能 心音分类在心血管疾病的早期发现中起着至关重要的作用,特别是对于小型初级卫生保健诊所.尽管近年来心音分 ...

  6. Go语言的包(package)

    包名是从$GOPATH/src/后开始计算的,使用/进行路径分隔. 想要被别的包调用标识符都要的首字母大. 单行导入和多行导入. 导入包不想使用内部的标识符,需要使用匿名导入. 每个包导入的时候会自动 ...

  7. ansible系列(29)--ansible的Jinja2语法及应用

    目录 1. Ansible Jinja2 1.1 jinja2语法结构 1.2 jinja2中{{ }}中的运算符 1.3 jinja2中for循环和if判断示例 1.4 Jinja2管理Nginx负 ...

  8. 防止XSS(跨站脚本攻击)漏洞

    点击查看代码 - 输入验证和过滤:对于用户输入的数据,进行严格的验证和过滤.可以使用正则表达式或其他验证方式,确保输入的数据符合预期的格式和内容.同时,对于特殊字符进行转义处理,防止恶意代码的注入. ...

  9. MSP 通过 Splashtop SOS 远程支持非托管设备

    RMM 是 MSP 的绝佳工具.它们使 MSP 可以通过集中控制台来管理其所有客户计算机,通常使他们能够远程访问任何计算机以提供远程支持. 但是,这样做的一个很大限制是--并非所有客户设备都在 MSP ...

  10. saltstack web 平台开发

    运维平台参考: https://wrapbootstrap.com/