[POJ1338]Ugly Numbers

试题描述

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.

输入

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

输出

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

输入示例

1
2
9
0

输出示例

1
2
10

数据规模及约定

见“输入

题解

首先与处理一下前 1500 个“丑数”,建立一个堆,对于一个“丑数” x,我们把 x * 2, x * 3, x * 5 都扔进堆,输出一下发现居然没有爆 unsigned long long(事实上连 int 都没爆),所以就是思博题了。

查询就访问数组即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {if(Head == Tail) {int l = fread(buffer, 1, BufferSize, stdin);Tail = (Head = buffer) + l;}return *Head++;
}
int read() {int x = 0, f = 1; char c = Getchar();while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }return x * f;
}#define maxn 1510
#define ULL unsigned long long
int n;
ULL num[maxn];
priority_queue <ULL> Q;int main() {num[1] = 1;Q.push(-2ll); Q.push(-3ll); Q.push(-5ll);for(int i = 2; i <= 1500; i++) {num[i] = -Q.top(); Q.pop();while(num[i] == num[i-1]) num[i] = -Q.top(), Q.pop();Q.push(-(num[i] << 1ll)); Q.push(-num[i] * 3ll); Q.push(-num[i] * 5ll);}while(1) {n = read();if(!n) break;printf("%llu\n", num[n]);}return 0;
}

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5803054.html

[POJ1338]Ugly Numbers相关推荐

  1. POJ1338 Ugly Numbers(解法二)【废除!!!】

    本文废除!!! 参考链接:POJ1338 Ugly Numbers[水题] 问题链接:POJ1338 Ugly Numbers.基础级练习题,用C语言编写程序. 题意简述:不能被2.3和5以外的素数整 ...

  2. POJ1338 Ugly Numbers

    问题链接:POJ1338 Ugly Numbers.基础级练习题,用C++语言编写程序. 题意简述:不能被2.3和5以外的素数整除的数称为丑数,找出第1500个丑数. 问题分析:换句话说,丑数的因子只 ...

  3. poj 1338 Ugly Numbers(丑数模拟)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem?id=1338 Descr ...

  4. 136 - Ugly Numbers

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

  5. Poj 1338 Ugly Numbers(数学推导)

    一.题目大意 本题要求写出前1500个仅能被2,3,5整除的数. 二.题解 最初的想法是从1开始检验该数是否只能被2,3,5整除,方法是这样的,对于一个数,如果它能被2整除,就除以2,如果它能被3整除 ...

  6. Ugly Numbers问题

    算法一 Ugly Numbers 一.问题描述       质因子只有2.3.5的数称为丑数,为了方便,1也被归为丑数(1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15-)     ...

  7. UVA - 136:Ugly Numbers

    Ugly Numbers 来源:UVA 标签: 参考资料:<算法竞赛入门经典>P120 相似题目: 题目 Ugly numbers are numbers whose only prime ...

  8. 【例题5-7 UVA - 136】Ugly Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个丑数x,都能生成3个丑数2x,3x,5x 则我们以1作为起点. 生成丑数. 每次取出set里面最小的那个数. 然后用它去生成其他 ...

  9. 比紫书优化,14行代码AC——例题 5-7 丑数(Ugly Numbers,UVa 136)——解题报告

    题意: 丑数是一些因子只有2,3,5的数.数列1,2,3,4,5,6,8,9,10,12,15--写出了从小到大的前11个丑数,1属于丑数.现在请你编写程序,找出第1500个丑数是什么. 没有输入 输 ...

最新文章

  1. 汇编语言(二十二)之统计减去奇数的个数
  2. 领导者的资质——学习笔记(2):领导者的人格
  3. Android 应用开发(42)---ImageView
  4. 项目入口_住宅小区入口就该这么设计,说得好仔细!
  5. NuGet无法下载的问题
  6. nginx + lua 构建网站防护waf(一)
  7. 华为手机鸿蒙系统下载,华为鸿蒙系统手机版
  8. 运行QT打包后的程序出现d:\Program Files (x86)\SogouInput\...错误
  9. Ubuntu修改键盘映射
  10. 3d效果图制作傻瓜软件_装修房子自己做3D效果图,哪些3D室内设计软件比较合适?...
  11. wgs84坐标格式转换度分秒_关于度分秒到数字度和几大坐标转换的工具类
  12. 0.1 番外篇: UML 类图
  13. js清除网页广告代码
  14. python输入个人所得税计算_Python实现的个人所得税计算器示例
  15. 电脑检测工具eve_检测电脑硬件的软件(系统硬件检测工具)
  16. 在做了在做了!次世代迭代进展让你一睹为快!【新倩女幽魂OL】
  17. 种草软文怎么写?分享一些超实用的种草软文写作技巧。
  18. log文件过大处理方法
  19. YOLOv5剪枝 | 模型剪枝理论篇
  20. M415步进电机驱动器的使用(C51)

热门文章

  1. 【Qt】QModbusRtuSerialMaster类
  2. 【Linux网络编程】Linux多播问题(No such device)解决方法
  3. 获取用户列表为空_Python中最常见的10个列表操作
  4. mysql 5.5.安装包_完美!阿里内部MySQL笔记爆火,肝完不再删库到跑路!(文档+视频贼全!)...
  5. on java 8 学习笔记 2022.2.17-2022.2.18
  6. 合并K个排序链表—leetcode23
  7. 使用 inline 内联函数对抗逆向分析
  8. MFC改变static text颜色
  9. 下载文件的响应头设置
  10. 多个Activity与Fragment之间的数据传递