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 6 为什么网络请求不跟随系统网络代理变化而动态切换代理
本文记录在 dotnet 6 的网络和在 .NET Framework 的行为的变更.在 dotnet 6 下,默认的网络请求在系统网络代理变更的时候,是不会动态切换代理的.例如在应用运行进行网络通讯 ...
- 实验8 #第8章 Verilog有限状态机设计-3 #Verilog #Quartus #modelsim
3. 状态机A/D采样控制电路 3.1 目标:用状态机控制ADC0809实现数据采集. 3.2 ADC0809简介 (1)ADC0809是8位A/D转换器,片内有8路模拟开关,可控制8个 模拟量中 的 ...
- ITIL4中的关键概念
1.价值和价值共创 什么是价值 通俗表达:这有啥用? 正式表达:这能带来什么益处或起什么作用? 反问式求证: 假如没有的话,会有什么后果? 具体情境提问:如果缺少IT运维人员,业务系统会面临怎样的状况 ...
- protobuf 文档
文档地址: https://golang-tech-stack.com/tutorial/pb 学习视频: https://www.bilibili.com/video/BV1Y3411j7EM?p= ...
- python教程6.5-excel处理模块
第三方开源模块安装 创建文件 打开已有文件 写数据 选择表 保存表 遍历表 按行遍历 按列遍历 遍历指定行列 遍历指定第几列数据 删除表 设置单元格样式 字体 对齐 设置行高列宽
- leaflet 在地图上创建marker标记
<!DOCTYPE html> <html> <head> <title>Layers Control Tutorial - Leaflet</t ...
- 前瞻 PHP8.4 的新特性
前瞻 PHP8.4 的新特性 PHP 8.4 将于 2024 年 11 月 21 日发布.它将包括属性钩子.JIT 改进,以及在不需要额外括号的情况下链式调用方法.这是一个大变化! 属性钩子 RFC ...
- JS 实现鼠标框选(页面选择)时返回对应的代码或文本内容
JS 实现鼠标框选(页面选择)时返回对应的代码或文案内容 一.需求背景 1.项目需求 当用户进行鼠标框选选择了页面上的内容时,把选择的内容进行上报. 2.需求解析 虽然这需求就一句话的事,但是很显然, ...
- Vue cli之项目打包
在项目根目录中执行如下命令: npm run build 注:Vue脚手架打包的项目必须在服务器上运行,不能直接双击运行: 在打包之后项目中出现 dist 目录,dist 目录就是 Vue脚手架项目的 ...
- 通过axios实现数据请求
vue.js默认没有提供ajax功能的. 所以使用vue的时候,一般都会使用axios的插件来实现ajax与后端服务器的数据交互. 注意,axios本质上就是javascript的ajax封装,所以会 ...