Ugly numbers are numbers whose only prime factors are 2, 3 or 5.

The sequence1, 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 numbercomputed.

Sample OutputThe 1500'th ugly number is .

把只包含因子2、3和5的数称作ugly numbers

例如6、8都是丑数,但20不是,因为它包含因子4。

本题要求输出第1500个丑数,用set直接判重输出

WA了一次发现UVA不支持#include<buts/stdc++.h>…………

set的用法:set<类型>名字;

例如set<int>se;set<string>se;

自己定义的结构体什么的也可以往里面塞

基本操作对集合a中元素的有

插入元素:a.insert(1);

删除元素(如果存在):a.erase(1);

判断元素是否属于集合:if(a.find(1)!=a.end())

返回集合元素的个数:a.size()

将集合清为空集 :a.clear ()

set可以解决去重和排序问题。

set中每个元素最多只出现一次

set中的元素已经从小到大排序好

如何通过迭代器从小到大遍历所有元素 

for (set<string>::iterator i = d.begin(); i != d.end(); i++)  

cout << *i << endl;

#include <cstdio>
#include <set>
#include <algorithm>
using namespace std; set<long long>a;
int ref[3]={2,3,5};
int main()
{
int m=1;
a.insert(1);//默认1位第一个丑数
set<long long>::iterator it=a.begin();
//set<long long>::iterator it;一样的
//迭代器对象代表容器中的确定的地址 ,理解成将a这个容器的首地址给了it
while(m<=1600)
{
it=a.begin();
for(it;it!=a.end()&&m<=1600;it++)//从小到大遍历元素,中间多了一个判断m数值的条件
{
for(int j=0;j<3;j++)
{
long long tmp=(*it)*ref[j];//使值对应乘一遍2、3、5
if(a.find(tmp)==a.end())//find()--返回一个指向被查找到元素的迭代器,此处为判断整个set数组中是否有tmp
{
a.insert(tmp);//如果没有就添加tmp
m++;//丑数+1
}
}
}
}
m=1;
it=a.begin();
for(it;it!=a.end()&&m++<1500;it++);//从小到大遍历set数组,注意此处的;
//一直到m=1499的时候,数组才停止,此时it++,正好对应了第1500个
printf("The 1500'th ugly number is %lld.\n",*it);//nice,完美,没毛病
return 0;
}

UVA - 136 Ugly Numbers (有关set使用的一道题)的更多相关文章

  1. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  2. 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 ...

  3. UVa 136 Ugly Numbers【优先队列】

    题意:给出丑数的定义,不能被除2,3,5以外的素数整除的的数称为丑数. 和杭电的那一题丑数一样--这里学的紫书上的用优先队列来做. 用已知的丑数去生成新的丑数,利用优先队列的能够每次取出当前最小的丑数 ...

  4. UVa 136 - Ugly Numbers

    题目大意:只有素因子2,3,5的数叫做丑数.输出第1500个丑数即可. 这个...好吧,直接输出就是了.自己写一个小程序先计算一下,这就是黑盒测试的好处啊,“我们的目标是解决问题,而不是为了写程序而写 ...

  5. 136 - Ugly Numbers

     Ugly Numbers  Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...

  6. 136 Ugly Numbers(priority_queue+逆向求解要求数)

    题目链接: https://cn.vjudge.net/problem/UVA-136 /*问题 输出第1500个丑数,丑数的定义是不能被2,3,5以外的其他素数整除的数 解题思路 直接硬暴力先试一下 ...

  7. 丑数(Ugly Numbers, UVa 136)

    丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...

  8. 【UVA - 136】Ugly Numbers(set)

    Ugly Numbers Descriptions: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...

  9. Ugly Numbers UVA - 136(优先队列+vector)

    Problem Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, ...

随机推荐

  1. 面试题:基础数据类型 包装类 int Integer

    因为在学习集合时知道集合里存放的对象都是Object类型,取出的时候需要强制类型转换为目标类型(使用泛型集合不需要),如int a = (Integer)arrayList.get(0):然后我们就会 ...

  2. c语言入门教程

    https://www.youtube.com/playlist?list=PLY_qIufNHc293YnIjVeEwNDuqGo8y2Emx 感觉这个教程不错

  3. Logistic Regression 用于预测马是否生病

    1.利用Logistic regression 进行分类的主要思想 根据现有数据对分类边界线建立回归公式,即寻找最佳拟合参数集,然后进行分类. 2.利用梯度下降找出最佳拟合参数 3.代码实现 # -* ...

  4. Flask框架 之 wtforms

    简介 WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 作用 生成HTML标签 form表单验证 使用 - 用户登录示例- 用户注册示例- 数据库获取数据实时更新 ...

  5. asp.net服务器推送长连接

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  6. Cloud Design Patterns: Prescriptive Architecture Guidance for Cloud Applications

    January 2014 Containing twenty-four design patterns and ten related guidance topics, this guide arti ...

  7. 关于Pascal(帕斯卡)以及Camel(驼峰)命名法

    小驼峰式命名法(lower camel case): 第一个单字以小写字母开始:第二个单字的首字母大写,例如:firstName.lastName,也被称为Camel命名法. 大驼峰式命名法(uppe ...

  8. eclipse导入tomcat

    官网下载tomcat压缩包 官网:http://tomcat.apache.org/ tomcat8:链接:http://pan.baidu.com/s/1kVHAEoR 密码:kyt8 tomcat ...

  9. MVC 异常过滤

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  10. c++缓冲区------c++ Primer Plus

    通常,通过使用缓冲区可以更高效地处理输入和输出.缓冲区是用作中介的内存块,它是将信息从设备传输到程序或从程序传输给设备的临时存储工具.通常,像硬盘驱动器这样的设备以512字节(或更多)的块为单位来传输 ...