HDU 1058 优先队列or堆
本来应当是一道优先队列或者堆的题 因为每个数都应该是已经得到的数*2 *3 *5 *7而得到的 但是 2*7 大于 3*2 这就必须保证每次取得都是没有拿过的最小的数
但是它主动降低难度在样例里卖了个萌 n的范围是1~5842 而第5842在样例里给出了..所以我们在取出一个数 求出它的*2 *3 *5 *7的时候做一下判断 如果大于最后一位就直接break 因为相乘的顺序在 可以省一点时间
在判断某个数是否出现过的时候 开不出那么大的vis数组 所以直接for循环从ans数组中寻找 所幸没有超时QAQ
尤其需要注意的是题目的思考并不难 但是输出的英语用法是坑
1 2 3 分别是 first second third 所以缩写的是 st nd rd
21 22 23 等 是 twenty - first twenty - second twenty- third 等 它们的缩写都是 st nd rd
但是11 12 13 不是.. 它们的第次缩写都是 th
(我的输出代码写得判断很麻烦..因为我懒得改..)
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
long long int e[6050];
int w;
bool find(long long int x)
{
for(int i=0;i<w;i++)
{
if(e[i]==x)
return true;
}
return false;
}
void init()
{
w=0;
queue<long long int >q;
q.push(2);
q.push(3);
q.push(5);
q.push(7);
e[w++]=1;
e[w++]=2;e[w++]=3;e[w++]=5;e[w++]=7;
while(!q.empty())
{
long long int z=q.front();q.pop();
/* if(w==5842)
return ;*/
for(int i=0;i<4;i++)
{
long long int x=z;
///if(x> 2000000000)continue;
if(i==0)
{
x*=2;
if(x> 2000000000)
break;
if(!find(x))
{
e[w++]=x;
q.push(x);
}
}
else if(i==1)
{
x*=3;
if(x> 2000000000)
break;
if(!find(x))
{
e[w++]=x;
q.push(x);
}
}
else if(i==2)
{
x*=5;
if(x> 2000000000)
break;
if(!find(x))
{
e[w++]=x;
q.push(x);
}
}
else if(i==3)
{
x*=7;
if(x> 2000000000)
break;
if(!find(x))
{
e[w++]=x;
q.push(x);
}
} }
} }
int main(){
init();
int n;
sort(e,e+w);
while(~scanf("%d",&n))
{
if(n==0)
break;
if(n==11)
printf("The 11th humble number is ");
else if(n==12)
printf("The 12th humble number is ");
else if(n==13)
printf("The 13th humble number is ");
else
{
if(n%10!=1&&n%10!=2&&n%10!=3)
{
printf("The %dth humble number is ",n);
}
else if(n%10==1&&n%100!=11)
{
printf("The %dst humble number is ",n);
}
else if(n%10==2&&n%100!=12)
{
printf("The %dnd humble number is ",n);
}
else if(n%10==3&&n%100!=13)
{
printf("The %drd humble number is ",n);
}
else
printf("The %dth humble number is ",n);
}
printf("%I64d.\n",e[n-1]);
}
}
HDU 1058 优先队列or堆的更多相关文章
- CJOJ 2484 函数最小值 / Luogu 2085 函数最小值(STL优先队列,堆)
CJOJ 2484 函数最小值 / Luogu 2085 函数最小值(STL优先队列,堆) Description 有n个函数,分别为F1,F2,...,Fn.定义 \(Fi(x)=Aix^2+Bix ...
- hdu 1058 dp.Humble Numbers
Humble Numbers Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...
- JAVA数据结构--优先队列(堆实现)
优先队列(堆)的定义 堆(英语:Heap)是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的 ...
- HDOJ(HDU).1058 Humble Numbers (DP)
HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...
- HDU 1058 Humble Numbers(离线打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 解题报告:输入一个n,输出第n个质因子只有2,3,5,7的数. 用了离线打表,因为n最大只有58 ...
- 深入浅出数据结构C语言版(15)——优先队列(堆)
在普通队列中,元素出队的顺序是由元素入队时间决定的,也就是谁先入队,谁先出队.但是有时候我们希望有这样的一个队列:谁先入队不重要,重要的是谁的"优先级高",优先级越高越先出队.这样 ...
- HDU 2176 取(m堆)石子游戏 (尼姆博奕)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176 m堆石子,两人轮流取.只能在1堆中取.取完者胜.先取者负输出No.先取者胜输出Yes,然后输出怎 ...
- HDU 1058(打表)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1058 Humble Numbers Time Limit: 2000/1000 MS (Java/O ...
- 优先队列(堆) -数据结构(C语言实现)
数据结构与算法分析 优先队列 模型 Insert(插入) == Enqueue(入队) DeleteMin(删除最小者) == Dequeue(出队) 基本实现 简单链表:在表头插入,并遍历该链表以删 ...
随机推荐
- window常用软件
ftpserver QQ asc pan 屏保 view putty 迅雷 teamviewer绿色 teamviewer单文件 魔方 chrome winscp WinRAR xshell 鲁大师 ...
- stm32——RTC实时时钟
stm32——RTC实时时钟 一.关于时间 2038年问题 在计算机应用上,2038年问题可能会导致某些软件在2038年无法正常工作.所有使用UNIX时间表示时间的程序都将将受其影响,因为它们以自19 ...
- shell定时任务
1.认识Croncron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务:/sbin/se ...
- /bin/dd if=/path/to/source-file of=/path/to/backup-file
[root@ok virhost]# qemu-img info 05t.img image: 05t.img file format: raw virtual size: 10G (10737418 ...
- Java Hour 34 Weather ( 7 ) struts2 – validate
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 34 Form Validation 一般Form 提交都有验证的, ...
- 谷歌开源项目Chromium的源码获取与项目构建(Win7+vs10/vs13)
转自:http://blog.csdn.net/kuerjinjin/article/details/23563059 从12年那会儿开始获取源码和构建chromium项目都是按照那时候的官方要求用w ...
- Android 返回键双击退出程序
/** * 菜单.返回键响应 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == K ...
- Windows计数器做性能监控(window server 2008服务器)
使用Windows计数器 一.创建数据收集器集 二.创建数据收集器 三.使用数据收集器 1.修改数据收集器的属性 2.手动启用.手动停止数据收集器集 3.计划任务 4.在性能监视器中查看 一.性能监视 ...
- 并查集+拓扑排序 赛码 1009 Exploration
题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...
- Windows Phone 7 播放视频
在Windows Phone 7中播放视频有两种方式,一种是使用MediaElement 控件来播放,一种是使用启动器MediaPlayerLanucher来实现视频的播放.用MediaElement ...