UVA 136 Ugly Numbers
Posted 韵祈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 136 Ugly Numbers相关的知识,希望对你有一定的参考价值。
原题代号:UVA 136
题目原题:
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, 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
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>.
题目不难就是输出语句没看懂。。。提交时候错了好多次。。。
题目大意:定义一种类型的数字叫做“丑数”他仅仅由2,3,5这三个质数构成的数叫做丑数,并且额外定义1也是丑数,所以丑数前11项是:1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 让你求第1500个丑数的值。
题目理解:对于每一个质数2 3 5,我们都要找到对应的之前已经计算出的最小的丑数使之乘以这个质数后大于当前的丑数,然后再从这三个里取最小的那个,我们记录三个数i,j,k,分别保存对应的质数计算到哪个下标了,然后更新所有质数对应的下标,使之乘积大于当前的丑数。
AC代码:
# include <stdio.h> # include <string.h> # include <stdlib.h> # include <iostream> # include <fstream> # include <vector> # include <queue> # include <stack> # include <map> # include <math.h> # include <algorithm> using namespace std; # define pi acos(-1.0) # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define For(i,n,a) for(int i=n; i>=a; --i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define Fo(i,n,a) for(int i=n; i>a ;--i) typedef long long LL; typedef unsigned long long ULL; int a[1505]={0,1}; int main() { int i=1,j=1,k=1,ans=1; for(int t=2;t<=1500;t++) { a[t]=min(min(2*a[i],3*a[j]),5*a[k]); if(2*a[i]==a[t])i++; if(3*a[j]==a[t])j++; if(5*a[k]==a[t])k++; } int n; printf("The 1500‘th ugly number is %d.\n",a[1500]); return 0; }
以上是关于UVA 136 Ugly Numbers的主要内容,如果未能解决你的问题,请参考以下文章
UVA - 136 Ugly Numbers(丑数,STL优先队列+set)