UVA 136 Ugly Numbers

原题代号:UVA 136

原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=72

题目原题:

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;
}

  

时间: 2024-08-08 13:58:15

UVA 136 Ugly Numbers的相关文章

UVa 136 Ugly Numbers(优先队列)

题意  质因数只可能有2,3,5的数称为丑数  输出第1500个丑数 STL优队列应用  1是丑数 丑数的2,3,5倍都是丑数  用优先队列模拟就行了 #include <cstdio> #include <cstring> #include <set> #include <queue> using namespace std; typedef long long ll; //priority_queue<ll, vector<ll>, g

UVA - 136 Ugly Numbers(丑数,STL优先队列+set)

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 Ther

UVa 136 Ugly Numbers【优先队列】

题意:给出丑数的定义,不能被除2,3,5以外的素数整除的的数称为丑数. 和杭电的那一题丑数一样--这里学的紫书上的用优先队列来做. 用已知的丑数去生成新的丑数,利用优先队列的能够每次取出当前最小的丑数再去生成新的丑数==== 大概这儿的优先队列就充当了dp转移方程里面的那个min的意思@[email protected] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<a

uva 101 Ugly Numbers

Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot arm performed tasks involving the m

UVA 136 &amp; 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. 把只含有2.3.5因数的数称为丑数,默认第一个丑数是1. POJ是有多次询问,输出第n个丑数 UVA是询问第1500个丑数是多少. 思路:

丑数(Ugly Numbers,UVA 136)

#include<iostream> #include<set> #include<vector> #include<queue> using namespace std; const int coeff[3]={2,3,5}; typedef long long LL; int main() { priority_queue<LL,vector<LL>,greater<LL> > pq; set<LL> s;

poj 1338 Ugly Numbers

Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21315   Accepted: 9520 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 num

Ugly Numbers(POJ1338)(丑数,技巧性强)

Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20851   Accepted: 9248 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 num

POJ 1338 Ugly Numbers dp

 Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21867   Accepted: 9767 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