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 ...
随机推荐
- C# 多态(2)
接上面讲 我们知道虚方法,和重写方法. 但是有没有发现 override和new的作用是差不多的. 但为什么还要单独写出来两个呢? 首先,咱们要明白一个问题,继承是具有线性传播的. class Fat ...
- Mac php7本地安装mongodb扩展以适配使用mongo扩展的线上老代码
从https://pecl.php.net/package/mongodb官网下载压缩包(不懂事的我下载了1.5.1版本) 解压安装包 tar -zxvf mongodb-1.5.1.tgz 进入解压 ...
- 第十一篇 logging模块
logging模块是Python中内置的很强大的一个日志模块,它可以帮我们记录程序运行的情况,对于后续排错有很好的帮助. logging模块定义了下表所示的日志级别,按照严重程度由低到高排列: 级别 ...
- python文件操作-修改文件中的内容
一.文件读写有缓冲区 fw = open('nhy','w') fw.write('sdfsdf') fw.flush()# 把缓冲区里面的数据立即写到磁盘上 fw.close() 二.with的用法 ...
- luoguP3302 [SDOI2013]森林
https://www.luogu.org/problemnew/show/P3302 看到查询第 k 小,而且是一颗树,可以联想到在树上的主席树,a 和 b 路径中第 k 小可以通过在 a, b, ...
- Python3用sys和time模块实现进度条
import sys import time def view_bar(num, total): rate = float(num) / float(total) rate_num = int(rat ...
- SDUT OJ 顺序表应用3:元素位置互换之移位算法
顺序表应用3:元素位置互换之移位算法 Time Limit: 1000 ms Memory Limit: 570 KiB Submit Statistic Discuss Problem Descri ...
- sqoop常用语句
1,列出全部数据库 sqoop list-databases --connect jdbc:sqlserver://10.10.10.2 --username sa --password 1 2,导 ...
- vscode 遇到设置问题
// 控制是否在新窗口中打开文件. // - default: 除了从软件内部选择的文件 (如,从“文件”菜单选择),其他所有文件都将在新窗口中打开 // - on: 文件将在新窗口中打开 // - ...
- linux的目录和基本的操作命令
目录相关操作:( ctrl+l 清空当前的屏幕中的命令 ) 一:目录说明: . 当前目录.. 上一层目录- 前一个工作目录~ 当前[用户]所在的家目录 蓝色的文件: 都是目录 白 ...