[ACM] POJ 1218 THE DRUNK JAILER (关灯问题)
版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/sr19930829/article/details/37727417
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 23246 | Accepted: 14641 |
Description
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
Output
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 (关灯问题)的更多相关文章
- poj 1218 THE DRUNK JAILER【水题】
THE DRUNK JAILER Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25124 Accepted: 1576 ...
- poj 1218 THE DRUNK JAILER
THE DRUNK JAILER Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23358 Accepted: 1472 ...
- POJ 1218 THE DRUNK JAILER(类开灯问题,完全平方数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2188 题目大意:n为5-100之间的一个数,代表有多少间牢房,刚开始所有房间打开,第一轮2的倍数的房间 ...
- 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 ...
- 北大ACM - POJ试题分类(转自EXP)
北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...
- HDU 1337 && POJ 1218&& zju 1350 方法总结
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1337 杭电 http://poj.org/problem?id=1218清华 http://acm.zj ...
- ACM: POJ 1401 Factorial-数论专题-水题
POJ 1401 Factorial Time Limit:1500MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- [ACM] POJ 2151 Check the difficulty of problems (概率+DP)
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4748 ...
- THE DRUNK JAILER
http://poj.org/problem?id=1218 题意:门的状态有两种开或关,初始化为开,每次进行状态转换,第一次把门号是1的倍数的门状态转换,第二次把门号是2的倍数的门状态转换,.... ...
随机推荐
- Git的安装配置
Git是什么 Git是一款免费.开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. 与常用的版本控制工具CVS.Subversion等不同的是它采用了分布式版本库的方式,不必 ...
- jq表单提交加正则验证
验证方法:姓名,手机,邮箱这些,鼠标点击移走用input的失去焦点blur事件.若为空,给input下方加提示消息. html: input框是引用bootstrap的css <div clas ...
- 想实现网页滚动一定距离底部弹出div
<script type="text/javascript"> $(window).scroll(function () { if ($(this).scrollTop ...
- 【知识强化】第六章 查找 6.3 B树和B+树
本节课我们来学习本章的第一个难点,就是B树.那么B树它其实是一种数据结构,我们设计出这种数据结构就是为了提高我们的查找效率的,提高我们在磁盘上的查找效率.那么什么是B树呢?了解B树之前,我们先来回忆一 ...
- ArrayListMultimap
遇到这样一个场景,就是要判断传过来的Lists里有几组相邻的元素,然后有多少个单一的元素,比如3,3,3,4,4,4,5,5,5,6,6,6,8,9,10,11 方法有很多,但是我选了ArrayLis ...
- The Preliminary Contest for ICPC Asia Nanjing 2019 D. Robots
题意:给出一个DAG,一只机器人从1点出发,等概率地选择一条出边走或者停留在原点,机器人的第k次行动消耗能量是k(无论是走还是停留都算一次行动).问1到n的期望. 解法:因为行动消耗的能量跟行动次数有 ...
- 编译lineageos1
lineageos 前奏 -- 搭建编译环境 我目前使用的手机是红米note4x 目前lineageos15.1已经官方支持,下文是按照官网文档编译安装包操作总结 构建环境搭建主要参考官方文档 参考文 ...
- 笔记73 高级SSM整合2
遇到的问题: 1.表单信息校验:jQuery前端校验,ajax用户名重复校验,重要信息后端校验(JSR303)+数据库约束 2.在设置下拉列表显示的值时出现问题. 3.邮箱也添加重复性校验 4.pub ...
- springboot解决跨域
@Configuration public class WebMvcConfiguration implements WebMvcConfigurer { @Bean public CorsFilte ...
- cytoscape.js 教程
因为数据要展示双向关系,最终选用了cytoscape.js. 效果如图,echarts只能显示单项数据关系. 据我理解,这个插件分两种,一种是基于jquery的,另一种是原声的. 基于jquery是加 ...