UVA 136 Ugly Numbers
2015-04-14 11:28
399 查看
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, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500'th ugly number.
Input
There is no input to this program. Output should consist of a single line as shown below, with <number> replaced by the number computed.
Sample
The 1500'th ugly number is <number>.
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500'th ugly number.
Input
and Output
There is no input to this program. Output should consist of a single line as shown below, with <number> replaced by the number computed.Sample
output
The 1500'th ugly number is <number>.#include<iostream> #include<cstdio> using namespace std; int ugly[1506]; int getmin(int a,int b,int c)//找出三个数中最小的 { if(a<=b) { if(a<=c) return a; else return c; } else { if(b<=c) return b; else return c; } } int find_ugly(int n) { int i2=1,i3=1,i5=1;//设定三个游标因子 ugly[1]=1; int i=2; int min; while(i<=n) { min=getmin(ugly[i2]*2,ugly[i3]*3,ugly[i5]*5); ugly[i]=min; //如果因子被用过,那么游标就加一,确保不重不漏 if(min==ugly[i2]*2) i2++; if(min==ugly[i3]*3) i3++; if(min==ugly[i5]*5) i5++; i++; } return ugly ; } int main() { int n; while(cin>>n) { cout<<find_ugly(n)<<endl; } return 0; }
相关文章推荐
- Uva136 Ugly Numbers 【优先队列】【例题5-7】
- UVA - 136 Ugly Numbers
- Ugly Numbers(set,queue,vector) -uva 136
- UVA - 136 Ugly Numbers
- UVA136 - Ugly Numbers(丑数)
- UVA 136 - Ugly Numbers【巧用优先队列】
- UVA - 136 Ugly Numbers (有关set使用的一道题)
- UVA136 - Ugly Numbers 优先队列
- uva 136 Ugly Numbers(priority_queue的使用)
- Uva136——Ugly Numbers
- Ugly Numbers UVA - 136
- UVa - 136 - Ugly Numbers(STL - 优先队列priority_queue)
- UVa 136 Ugly Numbers【优先队列】
- uva 136 ——Ugly Numbers
- Uva 136 Ugly Numbers(丑数)
- UVa 136 - Ugly Numbers
- UVa136 Ugly Numbers(优先队列+set判重)
- Ugly Numbers UVA - 136
- Uva 136 Ugly Numbers 优先队列
- Ugly Numbers(UVa 136)优先队列