【UVA - 136】Ugly Numbers(set)

Ugly Numbers

Descriptions:

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 ‘’ replaced by the number computed. 

Sample Output

The 1500'th ugly number is <number>.
题意
丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来,结果如下:
1,2,3,4,5,6,8,9,10,12,15……
求第1500个丑数

输入
没有输入
输出
The 1500'th ugly number is <number>.

题目链接:

https://vjudge.net/problem/UVA-136

不难发现2,3,5之后的丑数全是有这三个数乘上{2,3,5}之后得来的,这就好办了,因为不可重复,还需要按顺序来,所以选择<set>是没有问题的,用<set>存入这些丑数,记录 it 的大小,当it为1500-1(数组从0开始计数)时就是我们要找的这个丑数

AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
set<ll> num;//存入丑数
set<ll>::iterator it;
int main()
{
    int a[3]= {2,3,5};
    num.insert(1);
    int sum=0;//这里面肯定会有重复的,所以要重新设一个计数器
    it=num.begin();//从0开始计数
    while(sum<1500-1)//当然sum的值可以更大一点,这也没事,主要是去重复
    {
        for(int i=0; i<3; i++)
            num.insert(*it * a[i]);
        it++;//记录当前为第几个丑数
        sum++;
    }
    printf("The 1500'th ugly number is %llu.\n", *it);
}

 








 

转载于:https://www.cnblogs.com/sky-stars/p/10994824.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值