nyoj1032——Save Princess——————【set应用】
Save Princess
- 描述
-
Yesterday, the princess was kidnapped by a devil. The prince has to rescue our pretty princess."OK, if you want to save the beautiful princess, you must answer my questions correctly."the devil says."No problem!"."I’ll ask you t questions. For each question, I’ll tell you an integer n, you must tell me the i th beatuiful number. If your answer is wrong, the princess and you will all die"."But what is the characteristic of the beautiful number?" Pince asks."Beautiful numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, 10, ... shows the first 9 beautiful numbers.By convention, 1 is included. "Can you help the prince to save the princess?
- 输入
- The input for each case is an integer n(1≤n≤5000) and it is terminated by a negative integer.
- 输出
- For each test case, you should print an integer which represents the i th beautiful number.
- 样例输入
-
2
3
-1 - 样例输出
-
2
3 解题思路:用优先队列来存放美丽数,每次从队列中拿出最小的美丽数,由它扩展出三个美丽数,用set的去重功能来判断是否放入set和优先队列。依次从优先队列中出来的就是从小到大的美丽数。#include<stdio.h>
#include<string.h>
#include<set>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int f[4]={2,3,5};
LL a[5500];
void prin(int n){ priority_queue<LL,vector<LL>,greater<LL> >pq;
set<LL>s;
pq.push(1);
s.insert(1);
for(int i=1;;i++){ LL tm=pq.top();
a[i]=tm;
pq.pop();
if(i>n)
break;
for(int i=0;i<3;i++){ LL x=tm*f[i];
if(!s.count(x)){//返回元素x的个数 s.insert(x);
pq.push(x);
}
}
} }
int main(){ int n;
prin(5010);
while( scanf("%d",&n)!=EOF&&n>0){ printf("%lld\n",a[n]);
}
return 0;
}
nyoj1032——Save Princess——————【set应用】的更多相关文章
- Save Princess(丑数)
Save Princess 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Yesterday, the princess was kidnapped by a de ...
- ZOJ 3369 Saving Princess
Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...
- [转]Laravel 4之路由
Laravel 4之路由 http://dingjiannan.com/2013/laravel-routing/ Laravel 4路由是一种支持RESTful的路由体系, 基于symfony2的R ...
- NHibernate初步使用
1.创建一个网站项目:QuickStart 2.引用程序集:NHibernate.dll 3.更改配置文件加入以下节点: <configSections> <section name ...
- C# 数据操作系列 - 10 NHibernate初试
0. 前言 在上一篇基本讲完了EF Core的入门级教程.从这一篇开始,我们试着去探索一下 .net core平台上更多的ORM框架.那么,这一篇开始我们就来试试NHibernate. 1. NHib ...
- sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- 山东省第四届acm.Rescue The Princess(数学推导)
Rescue The Princess Time Limit: 1 Sec Memory Limit: 128 MB Submit: 412 Solved: 168 [Submit][Status ...
- sdutoj 2603 Rescue The Princess
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2603 Rescue The Princess ...
- SDUT 2603:Rescue The Princess
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
随机推荐
- Dynamically loading unmanaged OCX in C#
You'll have to perform a number of steps that are normally taken of automatically when you use the t ...
- 安卓开发时访问google方法
启动浏览器后15秒左右,浏览器的右上角就会出现图标 启用防火墙功能(右上角墙形图标),这时候程序就会去寻找网上代理,从而达到访问GOOGLE的效果,提示如果不访问google网站,可再点击一下关闭防火 ...
- IO模型《一》IO模型介绍
IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞 ...
- 【转】C#使用Oracle.ManagedDataAccess.dll
源地址:https://www.cnblogs.com/goldenbridge/p/7812081.html
- php 中 include 与 require 的区别
以下内容转自:https://blog.csdn.net/hsd2012/article/details/51089785 网上太多关于php中include与require区别.其实说的都是经不起验 ...
- 如何用python批量翻译文本?
首先,看一下百度翻译的官方api文档. http://api.fanyi.baidu.com/api/trans/product/apidoc # coding=utf-8 #authority:bi ...
- linux防火墙(一)—— iptables架构介绍
一.防火墙的分类 一般宏观来说,防火墙分为主机型防火墙,例如我们为了防止个人电脑被攻击,而开启的防火墙,还分为网关型防火墙,一般部署在企业的网关,用于过滤和转发,保证整个企业的网络环境安全性. 按照物 ...
- 中国版Azure 文件服务
中国版Azure 文件服务预览版在2015年1月13日已经上线,文件存储使用标准SMB 2.1协议为应用程序提供共享存储. 当我们的虚拟机连接到文件共享后就可以像使用本地共享文件夹一样来读取和写入文件 ...
- checkbox的常见问题
1.在使用checkbox时,最好不要阻止他原有的事件,要利用它原有的事件进行控制 2.尽量使用label for属性,不要对input元素的父元素或者input本身绑定事件,这样不能有效的避免冒泡事 ...
- zabbix 3.0 快速安装文档
下载地址:http://www.zabbix.com/download.php 官方文档:https://www.zabbix.com/documentation/3.0/manual/install ...