poj 1338 Ugly Numbers(丑数模拟)
来源:互联网 发布:新大教育网络管理系统 编辑:程序博客网 时间:2024/05/21 01:57
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://poj.org/problem?id=1338
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.
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
1290
Sample Output
1210
PS:用一个长度为1500的数组存储这些数,另有三个游标x,y,z;
a[1]=1,x=y=z=1,代表第一个数为1,此后的数都是通过已有的数乘以2,3,5得到的,
那么x,y,z分别代表a[x],a[y],a[z]可以通过乘以2,3,5来得到新的数,i递增,每次取2*a[x], 3*a[y], 5*a[z]
中的最小值,得到a[i]后,可以将对应的x(或y,z)右移,当然如果原本通过3*2得到6,那么2*3也能得到6,
因此可能x和y都需要递增。
详见代码:
#include <iostream>using namespace std;int min(int a, int b, int c){return min(a,min(b,c));}int main(){int a[1517];int x, y, z, i;x = y = z = 1, a[1] = 1;for(i = 2; i <= 1500; i++){a[i] = min(2*a[x],3*a[y],5*a[z]);if(a[i] == 2*a[x])x++;if(a[i] == 3*a[y])y++;if(a[i] == 5*a[z])z++;}int n;while(cin >> n && n){cout<<a[n]<<endl;}return 0;}
5 0
- poj 1338 Ugly Numbers(丑数模拟)
- POJ 1338 Ugly Numbers(丑数)
- poj1338 Ugly Numbers(丑数模拟)
- POJ 1338 Ugly Numbers 寻找丑数
- POJ-1338-Ugly Numbers-丑数
- POJ 1338 Ugly Numbers(丑数)
- Ugly Numbers(丑数)
- 丑数 Ugly Numbers
- Ugly Numbers --丑数
- UVA136 - Ugly Numbers(丑数)
- POJ 1338 Ugly Numbers(模拟)
- NYOJ1097 Ugly Numbers 【丑数】
- 158ugly numbers丑数
- Ugly Numbers(POJ1338)(丑数,技巧性强)
- POJ1338 Ugly Numbers 丑数 [数论]
- 1.5.8丑数(Ugly Numbers)
- Ugly Numbers UVA 136 丑数
- Uva 136 Ugly Numbers(丑数)
- OpenVPN多处理之-多队列TUN多实例
- 使用CocoaPods管理依赖库
- Fighting
- 在IE浏览器将网页中的附件保存至本地时,文件名字变为乱码
- 【Nutch2.2.1基础教程之2.2】集成Nutch/Hbase/Solr构建搜索引擎之二:内容分析
- poj 1338 Ugly Numbers(丑数模拟)
- 移植 libuv 到 Visual C++ 6.0 并支持在 Windows XP 系统下编译
- android:两个应用之间如何传值之activity
- 夜袭庞龙鸟
- Ubuntu 查看和杀死进程
- jsp显示当前日期
- C++<algorithm>中sort的比较函数写法
- LeetCode Permutation Sequence
- ios 翻牌效果