版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/sr19930829/article/details/37727417

THE DRUNK JAILER
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23246   Accepted: 14641

Description

A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked.
One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey,and then runs down the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the
hall locking every other cell (cells 2, 4, 6, ?

). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, ?). If the cell is locked, he unlocks it; if it is unlocked, he locks it. He
repeats this for n rounds, takes a final drink, and passes out.
Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately escape.
Given the number of cells, determine how many prisoners escape jail.

Input

The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a single integer between 5 and 100, inclusive, which is the number of cells n.

Output

For each line, you must print out the number of prisoners that escape when the prison has n cells.

Sample Input

2
5
100

Sample Output

2
10

Source

 

解题思路:

题意为n个监狱,编号1到n,初始均关闭,进行n局游戏,第一局,把全部的监狱都打开,第i(i>=2)局,把编号为 i 的倍数的监狱的状态改变(打开变为关闭或关闭变为打开)。问n局游戏以后,有多少个监狱为打开状态。 用d[]数组来保存监狱的状态。模拟n局游戏就能够了。

代码:

 

#include <iostream>
#include <string.h>
#include <stack>
#include <iomanip>
#include <cmath>
using namespace std;
bool d[110]; int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
int cnt=0;
memset(d,0,sizeof(d));
for(int i=2;i<=n;i++)
{
for(int j=i;j<=n;j+=i)
d[j]=1-d[j];//状态改变
}
for(int i=1;i<=n;i++)
if(d[i]==0)
cnt++;
cout<<cnt<<endl;
}
return 0;
}

 

[ACM] POJ 1218 THE DRUNK JAILER (关灯问题)的更多相关文章

  1. poj 1218 THE DRUNK JAILER【水题】

    THE DRUNK JAILER Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25124   Accepted: 1576 ...

  2. poj 1218 THE DRUNK JAILER

    THE DRUNK JAILER Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23358   Accepted: 1472 ...

  3. POJ 1218 THE DRUNK JAILER(类开灯问题,完全平方数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2188 题目大意:n为5-100之间的一个数,代表有多少间牢房,刚开始所有房间打开,第一轮2的倍数的房间 ...

  4. THE DRUNK JAILER 分类: POJ 2015-06-10 14:50 13人阅读 评论(0) 收藏

    THE DRUNK JAILER Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24918   Accepted: 1563 ...

  5. 北大ACM - POJ试题分类(转自EXP)

    北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...

  6. HDU 1337 && POJ 1218&& zju 1350 方法总结

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1337 杭电 http://poj.org/problem?id=1218清华 http://acm.zj ...

  7. ACM: POJ 1401 Factorial-数论专题-水题

    POJ 1401 Factorial Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  8. [ACM] POJ 2151 Check the difficulty of problems (概率+DP)

    Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4748   ...

  9. THE DRUNK JAILER

    http://poj.org/problem?id=1218 题意:门的状态有两种开或关,初始化为开,每次进行状态转换,第一次把门号是1的倍数的门状态转换,第二次把门号是2的倍数的门状态转换,.... ...

随机推荐

  1. python第三方库安装失败处理方法

    各位道友,是不是在使用pip 命令安装第三方库遇到了以下情形呢? 这种情况可真让人头疼啊..经过几番周折,终于找到了认为最有效的解决方法 首先 先把要安装的包下载下来,不管用什么方式 在这里我用的迅雷 ...

  2. 哪些文件在vue项目中很重要,哪些可以删掉

    是时候告诉你重要文件是哪些了,这是一个陆游,所以 需要路由配置 index.js 路由配置文件是index.js注意这个文件最开始就在集成路由了 然后最开始集成路由的地方可以不写后缀名字,因此注意这个 ...

  3. python系列——文件操作

    打开和关闭 示例:python系列——文件操作的代码 打开模式 读取 写入

  4. 用html标签+css写出旋转的正方体

    有一段时间没写代码了,刚写有点手生,无从下手,为了能快速进入状态,就写了这一个小东西,纯用标签和样式表写.下面看一下我写的. 这一段是样式表: <style> *{ margin: 0; ...

  5. 【杂记】docker搭建ELK 集群6.4.0版本 + elasticsearch-head IK分词器与拼音分词器整合

    大佬博客地址:https://blog.csdn.net/supermao1013/article/category/8269552 docker elasticsearch 集群启动命令 docke ...

  6. 2018-2-13-关于Host(主机)

    title author date CreateTime categories 关于Host(主机) lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 ...

  7. shell函数的定义和调用

  8. Sass--伪类嵌套

    其实伪类嵌套和属性嵌套非常类似,只不过他需要借助`&`符号一起配合使用. a { &:link, &:visited { color: blue; } &:hover ...

  9. 对struct typedef *的认识

    typedef struct node { ……… }NODE,*PNODE; 应该等价于 typedef struct node NODE;//struct node = NODE,eg:struc ...

  10. filter 过滤emoji

    拦截器 public class EmojiFilter implements Filter { private FilterConfig filterConfig; public void init ...