Ugly Numbers
Ugly Numbers
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21918 Accepted: 9788
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
Source
New Zealand 1990 Division I,UVA 136
打表记录即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define LL long long
using namespace std;
const LL MAX = 1e15;
LL a[15500];
int main()
{
LL b,c,d;
int top=0;
//freopen("output.txt","w",stdout);
for(int i=0;i<=30;i++)
{
if(i==0)
{
b=1;
}
else
{
b*=2;
}
if(b>MAX)
{
break;
}
for(int j=0;j<=30;j++)
{
if(j==0)
{
c=1;
}
else
{
c*=3;
}
if(b*c>MAX)
{
break;
}
for(int k=0;k<=30;k++)
{
if(k==0)
{
d=1;
}
else
{
d*=5;
}
if(b*c*d>MAX)
{
break;
}
a[top++]=b*c*d;
//printf("%lld\n",b*c*d);
}
}
}
sort(a,a+top);
int n;
while(scanf("%d",&n)&&n)
{
printf("%lld\n",a[n-1]);
}
return 0;
}
Ugly Numbers的更多相关文章
- [POJ1338]Ugly Numbers
[POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- poj 1338 Ugly Numbers(丑数模拟)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:id=1338&q ...
- leetcode@ [263/264] Ugly Numbers & Ugly Number II
https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...
- Geeks Interview Question: Ugly Numbers
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...
- 136 - Ugly Numbers
Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...
- Ugly Numbers(STL应用)
题目链接:http://poj.org/problem?id=1338 Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- 丑数(Ugly Numbers, UVa 136)
丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...
- UVA.136 Ugly Numbers (优先队列)
UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...
- UVA - 136 Ugly Numbers (有关set使用的一道题)
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...
随机推荐
- mysql:键缓存
myisam的主要优化参数: key_buffer_size - 这对MyISAM表来说非常重要,是用来设置整个MySQL中常规Key Cache的大小.一般来说,如果MySQL运行在32位平台,此值 ...
- Java线程同步和线程通信
一.线程同步 当多个线程访问同一个数据时,非常容易出现线程安全问题.这时候就需要用线程同步. 不可变类总是线程安全的,因为它的对象状态是不可改变的,但可变类对象需要额外的方法来保证线程安全. 1.同步 ...
- PostgreSQL Replication之第十三章 使用PL/Proxy扩展(1)
在这里添加一个slave,真的有一个很好的可扩展性的策略,这基本上足以满足大多数现代应用程序.使用一台服务器的情况下,许多应用程序就会完美地运行,您可能想添加以副本以给基础设施增加一些安全,但在许多情 ...
- FB面经prepare: Task Schedule
每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskS ...
- java 读取Excel文件并数据持久化方法Demo
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util ...
- windows系统调用 进程快照
#include "windows.h" #include "tlhelp32.h" #include "iostream" using n ...
- 夺命雷公狗jquery---4内容选择器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- SLC、MLC和TLC三者的区别
SLC=Single-LevelCell,即1bit/cell,速度快寿命长,价格超贵(约MLC3倍以上的价格),约10万次擦写寿命 MLC=Multi-LevelCell,即2bit/cell,速度 ...
- JS中数组的操作
1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...
- 《OpenGL着色语言》理解点记录三
“帧缓冲区”中的“帧”的含义? “帧”是连续图像中的一幅,3D可视化程序最终都是转化为一幅幅的图像输出在显示器上,这一幅幅的图像叫做叫“帧”. 解释“glBlendFunc(GL_SRC_AL ...